开发者

jQuery that works in Firefox but not in IE

开发者 https://www.devze.com 2023-03-20 06:25 出处:网络
Ok guys/girls. Below is some jQuery that runs in Firefox but no IE. I have no idea why it craps out in one and not the other.

Ok guys/girls.

Below is some jQuery that runs in Firefox but no IE. I have no idea why it craps out in one and not the other.

Anyone??

function SwapTextAssetforPOS() {

    $("*").each(function () {

        if ($(this).children().length == 0) {

            $(this).text($(this).text().replace('Assets', 'POS'));
            $(this).text($(this).text().replace('Asset', 'POS'));
            $(this).text($(this).text().replace('assets', 'POS'));
            $(this).text($(this).text().replace('asset', 'POS'));

        }

    });

}

Sorry folks - the error that I get is:-

SCRIPT65535: Unexpected call to method or property access. jquery-1.6.min.js, line 16 character 60352

EDIT:--------开发者_如何学Python----------------------------------------------------------------------------

Ok so an update - I removed the * selector and IE no longer blows up, my issue now is that I cant figure how to get it to do the replace on the element. I have the following code to ping up all the text elements in the object:

function SwapTextAssetforPOS() {
        var containerElementByID = $("#assetDetailContents");

        containerElementByID.children().children().each(function () {
            var $this = $(this);

            alert($this.text());


        });

This chucks me up an alert for every bit of text, however some is contained within a table, some is within a span, and some is just there. I have no control over a majority of this stuff so my new question is how do I get the previous replace to work using this type of selector. -- I can believe how painful this is..

Cheers again


I see the problem in my IE browser. When you do the $("*").each... it takes every single element in the page (including title, script, etc). When you do .text(), looks like it fails for some elements in IE for which .text() doesn't make sense. Replace "*" for "div" and it should work for the divs for example.
Maybe you could do something like
if ($(this).text()) {$(this).text($(this).text().replace('Assets', 'POS'));} to make sure the text() is defined for that element.

Still, going through the whole DOM is overkill. Can you add a class to the elements that can have the text?, like class="replaceable" so you could just do a $(".replaceable").text(...


Ok folks - so firstly thanks for the help.

I have resolved the issue by cobbling a number of suggestions together and by doing a little bit of investigative work.

In a nutshell IE was crapping out when it ran up against an tag. I no not why but this is where it fell over every time.

function SwapTextAssetforPOS() {

        var overlaycon = $("#jq-selectionHelper").find("*:not(img)"); //This line simply looks at the div surrounding the template and returns (to an array I believe) every element therein except for img tag
                                                                      //as this breaks in IE when tying to do the replace text stuff below. 

        overlaycon.each(function () {

            var $this = $(this);

            if ($this.children().length == 0) {
                $this.text($(this).text().replace('Assets', 'POS'));
                $this.text($(this).text().replace('Asset', 'POS'));
                $this.text($(this).text().replace('assets', 'POS'));
                $this.text($(this).text().replace('asset', 'POS'));
            }
        });
    }

This code runs and I believe is a lot more efficient than my original offering. Any further suggestions for performance re-factoring are welcome but thank the lord this is now working.

Thanks again for all the help.

Regards


This code has no problem as seen here . I tested in IE and FF both works fine

http://jsfiddle.net/wKWRC/

You should use F12 developer tool for IE and see what error you are getting that way others can know what exact problem is . You can debug script and see where you are getting error . IE is sensitive to javascript errors and its possible there is some error before you are calling this function .

http://msdn.microsoft.com/en-us/library/gg699336%28v=vs.85%29.aspx


Just a hunch but you might be running out of memory in IE.

First, using $('*') is never advisable, its better that you narrow it down with a selector like $('p').

Also, every time you call $(this) you create a new jQuery object, so if there are a lot of elements on your page you're making 9 objects every time.

The convention is to set $this = $(this) at the begining of the function so you only use one object.

function SwapTextAssetforPOS() {

    $("*").each(function () {

        var $this = $(this);

        if ($this.children().length == 0) {

            $this.text($this.text().replace('Assets', 'POS'));
            $this.text($this.text().replace('Asset', 'POS'));
            $this.text($this.text().replace('assets', 'POS'));
            $this.text($this.text().replace('asset', 'POS'));

        }

    });

}


try it like this

for (var i = 0, replacements = ['Assets','assets','asset','Asset']; i < 4; i++)
    $("*:contains('" + replacements[i] +"')").map(function() { this.innerHTML = this.innerHTML.replace(/asset(s){0,1}/igm, 'POS'); })
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号