比较文本与innerHTML IE7问题
我找不到解决 IE7 中的 innerHTML bug 的方法。我需要查看动态生成的 HTML 的内容,并在文本为“-1”时更改它。我正在使用原型 js 库,但找不到修复程序。有什么想法吗?
JS:
<script language="javascript" type="text/javascript">
Event.observe(window, 'load', function () {
var num = 1;
var allAccountInfoItems = $A('accountInfoItem');
var numofElements = (allAccountInfoItems.length);
for (var x = 0; x < numofElements; x++ )
{
var oldHTML = $('accountInfo').innerHTML;
var newHTML = "Unlimited";
if (oldHTML == "-1")
{
$('accountInfo').update(newHTML);
}
var oldId = $('accountInfo').id;
var numPlus = num++;
$('accountInfo').id = oldId + numPlus;
}
});
</script>
I can't find a work around for the innerHTML bug in IE7. I need to look at the contents of dynamicly generated HTML and change it if the text is "-1". I'm using the prototype js gallery but couldn't find a fix. Any ideas?
JS:
<script language="javascript" type="text/javascript">
Event.observe(window, 'load', function () {
var num = 1;
var allAccountInfoItems = $A('accountInfoItem');
var numofElements = (allAccountInfoItems.length);
for (var x = 0; x < numofElements; x++ )
{
var oldHTML = $('accountInfo').innerHTML;
var newHTML = "Unlimited";
if (oldHTML == "-1")
{
$('accountInfo').update(newHTML);
}
var oldId = $('accountInfo').id;
var numPlus = num++;
$('accountInfo').id = oldId + numPlus;
}
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试过修剪空白吗?文本内容周围可能有空白文本节点。
如果这不起作用,只需尝试
alert(oldHTML)
看看您获得的实际值是什么。旁注:您意识到
$A('accountInfoItem')
结果为["a", "c", "c", "o", "u", "n", "t ”、“I”、“n”、“f”、“o”、“I”、“t”、“e”、“m”]
?这是故意的吗?Did you try trimming whitespace? You may have whitespace text nodes around the textual content.
If that doesn't work, just try
alert(oldHTML)
to see what the value you are getting really is.Sidenote: you realize
$A('accountInfoItem')
results in["a", "c", "c", "o", "u", "n", "t", "I", "n", "f", "o", "I", "t", "e", "m"]
? Is this intentional?$A 将任何内容转换为数组。我认为您想将 $$() 与 CSS 选择器一起使用。在您的情况下,它将是
点,因为它可能是以 accountInfoItem 作为类名的元素列表。
$A transforms anything to an array. I think you wanted to use $$() with a CSS selector in it. In your case, it would be
The dot is because it may be a list of elements with accountInfoItem as class name.
获取innerHTML的正确(DOM友好)方法是element.childNodes[0].textContent
因此,在您的代码中,它将是:
您可能需要像Roatin提到的那样删除字符串,以确保您只有文本。
The proper (DOM friendly) way to get innerHTML is element.childNodes[0].textContent
So, in your code, it would be:
You'll probably want to strip the string as Roatin mentioned, to be sure you have only the text.