admin管理员组文章数量:1424079
I've got a little code snippet to 'hack' away at some templated code I can't fix normally.
<script>
jQuery( document ).ready(function() {
jQuery(".avatar").each(function() {
var text = jQuery(this).attr("src");
text = text.replace("-64x64.jpg", ".jpg");
text = text.replace("-80x80.jpg", ".jpg");
text = text.replace("-32x32.jpg", ".jpg");
text = text.replace("-28x28.jpg", ".jpg");
text = text.replace("-16x16.jpg", ".jpg");
text = text.replace("-128x128.jpg", ".jpg");
jQuery(this).attr("src", text);
});
});
</script>
Upon this script above firing in the browser I'm getting the following error in the console:
TypeError: text is undefined
text = text.replace("-64x64.jpg", ".jpg");
Racking my brain but ing up with nothing. Tried using var text; to try and define it at the start of the script and also tried using a different variable name in case it was conflicting with something both of which did nothing....
I've got a little code snippet to 'hack' away at some templated code I can't fix normally.
<script>
jQuery( document ).ready(function() {
jQuery(".avatar").each(function() {
var text = jQuery(this).attr("src");
text = text.replace("-64x64.jpg", ".jpg");
text = text.replace("-80x80.jpg", ".jpg");
text = text.replace("-32x32.jpg", ".jpg");
text = text.replace("-28x28.jpg", ".jpg");
text = text.replace("-16x16.jpg", ".jpg");
text = text.replace("-128x128.jpg", ".jpg");
jQuery(this).attr("src", text);
});
});
</script>
Upon this script above firing in the browser I'm getting the following error in the console:
TypeError: text is undefined
text = text.replace("-64x64.jpg", ".jpg");
Racking my brain but ing up with nothing. Tried using var text; to try and define it at the start of the script and also tried using a different variable name in case it was conflicting with something both of which did nothing....
Share Improve this question asked Sep 6, 2013 at 9:48 G-ForceG-Force 212 silver badges6 bronze badges 1- 1 Can you provide the html? – tliokos Commented Sep 6, 2013 at 9:49
1 Answer
Reset to default 7This means that at least one of the elements with class avatar
does not have a src
attribute. attr
returns undefined
if the element in question doesn't have the attribute at all.
You can put a guard in (if (text)
). Here's an example. Also note that there's zero reason to use jQuery(this).attr("src")
; just use this.src
:
jQuery( document ).ready(function() {
jQuery(".avatar").each(function() {
if (this.src) {
this.src = this.src.replace("-64x64.jpg", ".jpg")
.replace("-80x80.jpg", ".jpg")
.replace("-32x32.jpg", ".jpg")
.replace("-28x28.jpg", ".jpg")
.replace("-16x16.jpg", ".jpg")
.replace("-128x128.jpg", ".jpg");
}
});
});
You can also probably make that code a bit more robust using a regular expression:
jQuery( document ).ready(function() {
jQuery(".avatar").each(function() {
if (this.src) {
this.src = this.src.replace(/-(\d+x\d+)\.jpg$/, ".jpg");
}
});
});
That will replace -DIGITSxDIGITS.jpg
with .jpg
, without being specific about what the digits in each case are. \d
means "any digit", and the +
after it means "one or more".
本文标签: javascriptJquery TypeError text is undefinedStack Overflow
版权声明:本文标题:javascript - Jquery: TypeError: text is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745402276a2657092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论