简体   繁体   English

如何使用jQuery元素创建/附加生成HTML?

[英]How do I generate html using jQuery element creation/appending?

I need to generate HTML for a checkbox. 我需要为复选框生成HTML。 I generate the checkbox using the following function that uses js string concatenation to generate a large block of HTML: 我使用下面的函数来生成复选框,该函数使用js字符串串联来生成大型HTML块:

function getColvisCheckboxHTML(){
//  Generates the html for the checkboxes in the column visibility modal
var checkboxes = '';
var tableheaders = table.columns().header();
for(var i = 1;i < tableheaders.length;i++){
    var colindex = tableheaders[i-1].getAttribute('data-column-index');
    var colname = tableheaders[i-1].getAttribute('aria-label').split(':')[0];
    //  Don't include ID column, this column should always be visible
    if(colname != 'ID'){
         checkboxes += '<label class="col-md-2 checkbox-inline"><input type="checkbox" value="' +
            colindex + '">' + colname + '</label>';
    }
    //  Display flags in rows of 5
    if(i%6==0){
        checkboxes += '<br />';
    }
};
return checkboxes;
};

The function is then called using this code within an ajax call: 然后在ajax调用中使用以下代码调用该函数:

$('#colvis_chkboxes').html(getColvisCheckboxHTML());

I was informed that the way I generate the HTML is the improper method and that I should use jQuery element creation/appending instead for readability. 我被告知,生成HTML的方式是不正确的方法,我应该使用jQuery元素的创建/附加来提高可读性。 My question is what would my code look like using this method? 我的问题是使用这种方法后我的代码会是什么样? I started trying this other method, but this is all I could come up with: 我开始尝试其他方法,但这就是我能想到的:

$('<label>', {
   "class": "col-md-2 checkbox-inline"
 }).append(
   $('<input/>', {
      "type": "checkbox"

Any advice would be awesome. 任何建议都很棒。 Thanks! 谢谢!

Try to take a look at this documentation: http://api.jquery.com/append/ 尝试看看此文档: http : //api.jquery.com/append/

Also in your sample you can have something like this: 同样在您的示例中,您可以具有以下内容:

$("label.col-md-2 checkbox-inline").append("YOUR HTML HERE");

Hope that help. 希望对您有所帮助。

From w3 : The label itself may be positioned before, after or around the associated control. 从w3开始 :标签本身可以位于相关控件之前,之后或周围。

<label for="lastname">Last Name</label>
<input type="text" id="lastname" />

Try with: 尝试:

var container = $('<div>');
container.append('<label class="col-md-2 checkbox-inline"></label>);
container.append('<input type="checkbox" name="my_name" />);
// OR
var container = $('<div>');
$('<input />', { type: 'checkbox', id: 'cb_id', value: name }).appendTo(container);
$('<label />', { 'for': 'cb_id', text: name, class: 'col-md-2 checkbox-inline' }).appendTo(container);

Instead of defining and adding the markup as a string, you can add attributes and styles using $(jquery) expression . 您可以使用$(jquery) expression添加属性和样式,而不是将标记定义为字符串并将其添加为字符串。 This would eliminate the hassle of escaping characters or any syntax issues. 这将消除转义字符或任何语法问题的麻烦。

var ele = $('<input>').addClass("green").attr("value","test").css("color","green");
$('div').append(ele);

Here's an example : https://fiddle.jshell.net/a2n234eq/5/ 这是一个例子: https : //fiddle.jshell.net/a2n234eq/5/

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

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