suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript – So betten Sie Variablen in das Klassenattribut des HTML-Tags ein

        var id = snapshot.val().id;
        var content = snapshot.val().content;
        var textObj = '<p class="task-item">\
                        <input type="checkbox" />\
                        <span class="ui-icon ui-icon-clock"></span>\
                        <span class="task-content">'+content+'</span>\
                        <span class="task-detail">&nbspdetail</span>\
                        </p>';

Ich möchte die ID-Variable (die eine Zahl ist) in der ersten Zeile zum Klassenattribut des p-Tags in der dritten Zeile hinzufügen. Kann sie direkt eingebettet werden?

巴扎黑巴扎黑2737 Tage vor922

Antworte allen(4)Ich werde antworten

  • ringa_lee

    ringa_lee2017-06-26 10:54:52

        var id = snapshot.val().id;
        var content = snapshot.val().content;
        var textObj = '<p class="task-item'+id+'">\
                        <input type="checkbox" />\
                        <span class="ui-icon ui-icon-clock"></span>\
                        <span class="task-content">'+content+'</span>\
                        <span class="task-detail">&nbspdetail</span>\
                        </p>';

    Antwort
    0
  • typecho

    typecho2017-06-26 10:54:52

    你的问题实质是如何改变拼接出来的HTML模板,本质是拼接字符串。
    最笨的方法

    var textObj1 = '<p class="task-item"';
    var textObj2 = '>\
                        <input type="checkbox" />\
                        <span class="ui-icon ui-icon-clock"></span>\
                        <span class="task-content">'+content+'</span>\
                        <span class="task-detail">&nbspdetail</span>\
                        </p>';
    textObj = textObj1 + id + textObj2

    或者直接用ES6 字符串模板,这样比较符合你的变量嵌套。但兼容性会有问题。

    var textObj = `<p class="task-item${id}">\
                            <input type="checkbox" />\
                            <span class="ui-icon ui-icon-clock"></span>\
                            <span class="task-content">'+content+'</span>\
                            <span class="task-detail">&nbspdetail</span>\
                            </p>`;

    Antwort
    0
  • 巴扎黑

    巴扎黑2017-06-26 10:54:52

    由于你这个其实是JS拼接字符串,所以直接用JS的+将字符串拼接进去。
    既然你都知道怎么把content拼接进去了,那拼接id也是同理

    ES5

    var id = snapshot.val().id;
    var content = snapshot.val().content;
    var textObj = '<p class="task-item' + id + '">\
        <input type="checkbox" />\
        <span class="ui-icon ui-icon-clock"></span>\
        <span class="task-content">' + content + '</span>\
        <span class="task-detail">&nbspdetail</span>\
    </p>';

    当然,你也可以使用ES6的模板字符串,不过浏览器兼容性会有问题,具体如下:(注意,整个textObj`开始,以`结束)

    ES6

    var id = snapshot.val().id;
    var content = snapshot.val().content;
    var textObj = `<p class="task-item${id}">\
        <input type="checkbox" />\
        <span class="ui-icon ui-icon-clock"></span>\
        <span class="task-content">${content}</span>\
        <span class="task-detail">&nbspdetail</span>\
    </p>`;

    Antwort
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:54:52

    Addattribute 试试

    Antwort
    0
  • StornierenAntwort