Javascript 如何将变量插入变量字符串?

Javascript 如何将变量插入变量字符串?,javascript,jquery,Javascript,Jquery,我有这个代码,我需要设置一个唯一的标题: 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); }); 将变量更改为模板的排序: var tmpImg = '

我有这个代码,我需要设置一个唯一的
标题

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);
});

将变量更改为模板的排序:

var tmpImg = '<img src="/admin/icons/cross.png" title="$title" />';
上面对原始代码的更改很少,但更好的jQuery方式是:

$(this).hide().before($("<img />").attr("src", "/admin/icons/cross.png").attr("title", this.value));

$(this).hide()。在($)之前(“将变量更改为模板排序:

var tmpImg = '<img src="/admin/icons/cross.png" title="$title" />';
上面对原始代码的更改很少,但更好的jQuery方式是:

$(this).hide().before($("<img />").attr("src", "/admin/icons/cross.png").attr("title", this.value));

$(this).hide()。在($)之前(“您可以在字符串中放置某种占位符标记,然后使用
替换

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

$(this).find("tr td input").each(function(){
    $(this).hide().before(tmpImg.replace(TITLE_TOKEN, $(this).attr("value")));
});
var TITLE_标记='%%TITLE%%';
var tmpImg=“”;
$(this).find(“tr td input”).each(function(){
$(this.hide().before(tmpImg.replace(TITLE_TOKEN,$(this.attr(“value”)));
});


旁注:
$(this).attr('value')
通常写得更好
this.value
$(this.val()

您可以在字符串中放入某种占位符标记,然后使用
替换

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

$(this).find("tr td input").each(function(){
    $(this).hide().before(tmpImg.replace(TITLE_TOKEN, $(this).attr("value")));
});
var TITLE_标记='%%TITLE%%';
var tmpImg=“”;
$(this).find(“tr td input”).each(function(){
$(this.hide().before(tmpImg.replace(TITLE_TOKEN,$(this.attr(“value”)));
});


旁注:
$(this).attr('value')
通常写得更好
this.value
$(this.val()

这些字符串替换解决方案都是胡说八道。只需复制元素并直接在其上设置属性即可

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

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

这些字符串替换解决方案非常简单。只需复制元素并直接在其上设置属性即可

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

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

这就是我要做的,为了它的价值:

$(this).find("tr td input").each(function(){
    $('<img/>', {
        src: "/admin/icons/cross.png",
        title: this.value
    }).insertBefore(this).next().hide();
});
$(this).find(“tr td input”).each(函数(){

$('这就是我要做的,为了它的价值:

$(this).find("tr td input").each(function(){
    $('<img/>', {
        src: "/admin/icons/cross.png",
        title: this.value
    }).insertBefore(this).next().hide();
});
$(this).find(“tr td input”).each(函数(){

$(‘我觉得你用两种不同的方式拼写“variable”一词有点好笑——两种都不正确——:-)我觉得你用两种不同的方式拼写“variable”一词有点好笑——两种都不正确——:-)这看起来像我本想作为答案的内容。这看起来像我本想作为答案的内容。