开发者

jQuery anchor text reference not working in Internet Explorer

开发者 https://www.devze.com 2023-02-11 04:40 出处:网络
I have some anchors in HTML that have a class of \"attrs\": <a href=\"#\" class=\"attrs\">sample</a>

I have some anchors in HTML that have a class of "attrs":

<a href="#" class="attrs">sample</a>

I use this jQuery code to bind to those elements unobtrusively, and I am just doing a simple alert for test purposes:

$(".attrs").click(function() {
      alert($(this).attr('text'));
    });

This works just dandy in Firefox and Safari on Mac, fails unreliably in Firefox on Windows, and fails completely with an "undefined" in the alert on IE 7 & 8.

Note that if I change 'text' to 'href', then t开发者_JAVA百科he alert comes up appropriately.

This seems like very simple, very basic code. I can not imagine why/how Windows browsers would behave incorrectly on something so simple. Can anyone offer anything here?


You should call:

alert($(this).text());

To get the text content from that anchor node.


Remove the attr function, and you don't have to use jQuery to get to this:

$(".attrs").click(function() {
      alert(this.text);
    });

This seems to work.


Well, thats because you don't have a text attribute for the a element.

Try this, if you want to alert the content of a:

$(".attrs").click(function() {
      alert($(this).html());
});
0

精彩评论

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

关注公众号