比较文本与innerHTML IE7问题

发布于 2024-08-10 21:01:51 字数 757 浏览 2 评论 0原文

我找不到解决 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-08-17 21:01:51

您尝试过修剪空白吗?文本内容周围可能有空白文本节点。

$('accountInfo').innerHTML.strip()

如果这不起作用,只需尝试 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.

$('accountInfo').innerHTML.strip()

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?

夜空下最亮的亮点 2024-08-17 21:01:51

$A 将任何内容转换为数组。我认为您想将 $$() 与 CSS 选择器一起使用。在您的情况下,它将是

$('.accountInfoItem');

点,因为它可能是以 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

$('.accountInfoItem');

The dot is because it may be a list of elements with accountInfoItem as class name.

空心↖ 2024-08-17 21:01:51

获取innerHTML的正确(DOM友好)方法是element.childNodes[0].textContent

因此,在您的代码中,它将是:

var oldHTML = $('accountInfo').childNodes[0].textContent;

您可能需要像Roatin提到的那样删除字符串,以确保您只有文本。

var oldHTML = $('accountInfo').childNodes[0].textContent.strip();

The proper (DOM friendly) way to get innerHTML is element.childNodes[0].textContent

So, in your code, it would be:

var oldHTML = $('accountInfo').childNodes[0].textContent;

You'll probably want to strip the string as Roatin mentioned, to be sure you have only the text.

var oldHTML = $('accountInfo').childNodes[0].textContent.strip();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文