问题描述
我需要一种方法来识别HTML标记中的某些字符串。我知道字符串是什么,但它可能是文档中其他字符串的子字符串。为了找到它们,我输出一个特殊的分隔字符(目前使用 \032
)。在页面加载时,我们浏览HTML并记录字符串的位置,并删除分隔符。
不幸的是,大多数浏览器都显示分隔符,直到找到并全部删除它们。如果可能,我想避免这种情况。是否有一个字符或字符串将保存在HTML内容(所以评论不会工作),但不会对用户可见?它也需要是一个不太可能出现在字符串旁边的东西,所以像& nbsp;
这样的东西也不行。
编辑:对不起,我忘了提及字符串将属性,所以任何类型的标签将无法正常工作。
& zwnj;
- 零宽度非joiner(请参阅)
开(例如:& zwnj;& zwnj; mytext& zwnj;& zwnj;
编辑回复评论:适用于Firefox 3.请注意,您必须搜索实体的Unicode值。
< html>
< body>
< div id =test>
这是& zwnj;测试
< / div>
< scrip t type =application / javascript>
var myDiv = document.getElementById(test);
var content = myDiv.innerHTML;
var pos = content.indexOf(\\\);
alert(pos);
< / script>
< / body>
< / html>
I need a way to identify certain strings in HTML markup. I know what the strings are, but it is possible that they could be substrings of other strings in the document. To find them, I output a special delimiter character (currently using \032
). On page load, we go through the HTML and record the location of the strings, and remove the delimiter.
Unfortunately, most browsers show the delimiter character until we can find and remove them all. I'd like to avoid that if possible. Is there a character or string that will be preserved in the HTML content (so a comment wont work) but wont be visible to the user? It also needs to be something that is fairly unlikely to appear next to a string, so something like
wouldn't work either.
EDIT: Sorry, I forgot to mention that the strings will be in attributes, so any sort of tag wont work.
‌
- zero-width non-joiner (see http://htmlhelp.org/reference/html40/entities/special.html)
On the off chance that this already appears in your text, double it up (eg: ‌‌mytext‌‌
Edit in response to comment: works in Firefox 3. Note that you have to search for the Unicode value of the entity.
<html>
<body>
<div id="test">
This is a ‌test
</div>
<script type="application/javascript">
var myDiv = document.getElementById("test");
var content = myDiv.innerHTML;
var pos = content.indexOf("\u200C");
alert(pos);
</script>
</body>
</html>
这篇关于HTML中字符串的不可见分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!