开发者

.text & .attr in Jquery not working in IE

开发者 https://www.devze.com 2023-02-22 01:46 出处:网络
I am having some trouble getting a function to work in internet explorer (7-8). My problem is that I need to hide some links until a user logs in, I would do this server side; however, suffice to say

I am having some trouble getting a function to work in internet explorer (7-8). My problem is that I need to hide some links until a user logs in, I would do this server side; however, suffice to say I have no way of doing this.

My approach has been to load a set of text (login/logout) that will change server side and test for the logout value. I was doing this by loading a div by id in Jquery and using the .text() when IE 7-8 both refuse to run this ie dev tools told me that "object doesn't support this property or method" with a reference back to the line of code containing this lookup. Code posted belo开发者_StackOverfloww:

function logintest(){
        login_test= "Sign out";
        alert($('#log').text());
        login = $('#log').text();
        if(login.search(login_test) == -1){
            $('#hiddenBox').css('display','none');
        }
    };

The fun thing is that the alert runs properly and displays the right text string. Upon this failing I tried using the .attr() and got identical results. Any help would be great!

Jquery version: 1.4.4 Site: www.brainwellnesspro.com IE: 7-8 on win xp


Looking at your site I'd start by fixing the html to be valid, this isn't good:

<div id="log" name="<a href='http://www.brainwellnesspro.com/login.php' onclick=''>Sign in</a> or <a href='http://www.brainwellnesspro.com/login.php?action=create_account' onclick=''>Create an account</a>">
<a href='http://www.brainwellnesspro.com/login.php' onclick=''>Sign in</a> or <a href='http://www.brainwellnesspro.com/login.php?action=create_account' onclick=''>Create an account</a>
</div>

and since you already have jquery you can check the current text value of the login link like this

function logintest(){
   if($("#log a").first().text() !== "Sign out"){
     $('#hiddenBox').css('display','none');
   }
};


My problem is that I need to hide some links until a user logs in, I would do this server side; however, suffice to say I have no way of doing this.

Why Not? Surely your application should know if the user is logged in or not (and be able to render differently accordingly?)

anyway - without seeing your markup (with the context of the element with id='log' (what type of element is this???)....

if(login.search(login_test) == -1)

should probably be

if(login.search(login_test) == '-1')
0

精彩评论

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