开发者

$("#but0_0").attr(...) works but $(hid).attr(...) does not work when hid === "#but0_0"

开发者 https://www.devze.com 2022-12-21 04:39 出处:网络
On Safari 4.0, I have a check that prints \"????\" if hid === \"#but0_0\" Works: $(\"#but0_0\").attr(\"onmousedown\",function(){alert(\"Aha!\");});

On Safari 4.0, I have a check that prints "????" if hid === "#but0_0"

Works:

$("#but0_0").attr("onmousedown",function(){alert("Aha!");});

Doesn't work:

var id = "but"+y+"_"+x;
var hid = "#"+id;
if (hid === "#but0_0") console.debug("????");
$(hid).attr("onmousedown",function(){alert("Aha!");});

The console shows "????" so the code is getting hit and the variable is "the same" (from === string comp开发者_高级运维are point of view) as the string literal but apparently jQuery's $() operator knows the difference?

I have 30 divs to manipulate and I need to compute the id of the div at run time. Any ideas? It's probably something simple like pulling out a "string" from a "String" or some such JavaScript stupidity.


Give the following a shot:

$(hid).live('mousedown', function() {alert("Aha!");});

if your elements are created dynamically.


Strange that that's the case.

Why aren't you using bind() to attach your events?

$(hid).bind('mousedown', function () { /* something */ }); 
0

精彩评论

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