简体   繁体   English

将字符串转换为jquery元素并将其附加到DOM

[英]Converting a string into a jquery element and appending it to the DOM

I'm trying to do what the title says with the following (admittedly terribly written) code: 我正在尝试使用以下(公认地非常糟糕的)代码来实现标题所说的内容:

$('.right').append($('<li class="btn log-out-button"><a data-method="delete" data-remote="true" format="json" href="' + data.log_out_path + '" class="standout-primary" rel="no-follow>Sign Out</a></li>

I can't for the life of me figure out what I'm doing wrong with it, but all that shows up is the following HTML: 我一辈子都无法弄清楚我在做什么错,但所显示的只是以下HTML:

<li class="btn log-out-button></li>

Why is it ignoring the inner HTML I wrote in my string? 为什么忽略了我在字符串中编写的内部HTML?

You have this code 你有这个代码

$('.right').append($('<li class="btn log-out-button"><a data-method="delete" data-remote="true" format="json" href="' + data.log_out_path + '" class="standout-primary" rel="no-follow>Sign Out</a></li>

Missing barces/quotes , instead try this clean approach 缺少大barces/quotes ,请尝试使用这种干净的方法

var link = $('<a/>', {
    'data-method':'delete',
    'data-remote':'true',
    'format':'json',
    'href': data.log_out_path, // make sure you have 'data' in current scope
    'class':'standout-primary',
    'rel':'no-follow',
    'text':'Sign Out'
});
var li = $('<li/>', { 'class':'btn log-out-button' }).append(link);
$('.right').append(li);

An Example Here. 这里的一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM