admin管理员组文章数量:1335451
I've implemented an image switch in my website. It worked fine but I broke it by altering to my wishes. The code was as following:
if (src) { $$('#' + id + ' a.product-image img').first().setAttribute("src", src);}
And i've changed it to:
if (src) { $$('#' + id + ' a.product-image img').first().setAttribute("src", src);
$$('#' + id + ' a.popup-image img').first().setAttribute("src", src);
}
I simply added a image source that need to be changed. But when I run it on my site I get the following error:
Uncaught TypeError: Cannot read property 'setAttribute' of undefined
I've checked if the element exists and if it could by found by adding the following code to my script:
if('#' + id + 'popup-image' == 0){
console.log("fail");
}else{
console.log("found");
}
And it returns found everytime. I've changed my code to this:
$$('#' + id + ' a.popup-image img').first().setAttribute("src", src);
But then I get the following error:
Uncaught TypeError: Cannot read property 'first' of null
How do I fix this?
I've implemented an image switch in my website. It worked fine but I broke it by altering to my wishes. The code was as following:
if (src) { $$('#' + id + ' a.product-image img').first().setAttribute("src", src);}
And i've changed it to:
if (src) { $$('#' + id + ' a.product-image img').first().setAttribute("src", src);
$$('#' + id + ' a.popup-image img').first().setAttribute("src", src);
}
I simply added a image source that need to be changed. But when I run it on my site I get the following error:
Uncaught TypeError: Cannot read property 'setAttribute' of undefined
I've checked if the element exists and if it could by found by adding the following code to my script:
if('#' + id + 'popup-image' == 0){
console.log("fail");
}else{
console.log("found");
}
And it returns found everytime. I've changed my code to this:
$$('#' + id + ' a.popup-image img').first().setAttribute("src", src);
But then I get the following error:
Uncaught TypeError: Cannot read property 'first' of null
How do I fix this?
Share Improve this question asked Nov 11, 2014 at 12:48 user3164891user3164891 2982 gold badges4 silver badges14 bronze badges 4-
setAttribute
is a DOM node method. You are trying to use it on jQuery obj – A. Wolff Commented Nov 11, 2014 at 12:50 -
What is the
$$
function? Note thatif('#' + id + 'popup-image' == 0){
does not check if the element exists, it pares a string to the number 0, which won't ever be equal so will always log "found". – nnnnnn Commented Nov 11, 2014 at 12:52 - @nnnnnn I always use this method to only display something if an element exist. And it always works correctly – user3164891 Commented Nov 11, 2014 at 12:53
-
No it doesn't. Read my explanation of why it doesn't work again. Think about how evaluating a string could possibly check whether a DOM element exists (it can't). You can easily test this by changing the
'popup-image'
part of thatif
to'blah'
- it'll still log "found". You have to pass that string to a function that looks for the DOM element (e.g., to the$$()
function, whatever it is). – nnnnnn Commented Nov 11, 2014 at 13:01
2 Answers
Reset to default 3If you want to use the JavaScript setAttribute()
function, you have to access the HTMLElement within your jQuery object by referencing the first item of its array, [0]
, like so:
$('#' + id + ' a.product-image img')[0].setAttribute("src", src);
Calling first()
will return a jQuery object; with this approach you have to use attr()
, as there is no setAttribute()
method in jQuery.
Try:
$('#' + id + ' a.product-image img').eq(0).attr("src", src);
or:
$('#' + id + ' a.product-image img:first').attr("src", src);
or you can use get() which takes an index argument, and passing 0 will return first element:
$('#' + id + ' a.product-image img').get(0).attr("src", src);
To check if an element exists you should've used the code:
if($$('#' + id + 'popup-image').length > 0){
console.log("found");
}else{
console.log("fail");
}
To set the attribute you should
if (src) { $$('#' + id + ' a.product-image img').first().attr("src", src);
$$('#' + id + ' a.popup-image img').first().attr("src", src);
}
本文标签: javascriptjQuery Cannot read property 39setAttribute39 of undefinedStack Overflow
版权声明:本文标题:javascript - jQuery Cannot read property 'setAttribute' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339992a2456405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论