gpt4 book ai didi

javascript - 如何将变量插入变量字符串?

转载 作者:行者123 更新时间:2023-11-30 08:11:58 28 4
gpt4 key购买 nike

我有这段代码,我需要设置一个唯一的 title :

var tmpImg = '<img src="/admin/icons/cross.png" title="' + title + '" />';

$(this).find("tr td input").each(function(){
title = $(this).attr("value");
$(this).hide().before(tmpImg);
});

我想要发生的是,每次 each遍历 <input> , 它更新 title tmpImg 中的值字符串。我知道我可以像下面这样分离 img HTML,尽管我认为当我需要稍后在脚本中重用图像时这会变得困惑。

var tmpImg = '<img src="/admin/icons/cross.png" title="';

$(this).find("tr td input").each(function(){
title = $(this).attr("value");
$(this).hide().before(tmpImg + title + '" />');
});

最佳答案

这些字符串替换解决方案是疯狂的。只需复制该元素并直接在其上设置属性即可。

var tmpImg = $( '<img src="/admin/icons/cross.png" />' );

$(this).find( "tr td input" ).each(function() {
$(this).hide().before( tmpImg.clone().attr( 'title', this.value ) );
});

关于javascript - 如何将变量插入变量字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8969771/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com