English 中文(简体)
Compare text with innerHTML IE7 problem
原标题:

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>
问题回答

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?

$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.

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();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签