简体   繁体   English

jQuery中标签文本之前的复选框

[英]Checkbox before label text in jQuery

I have the following function: 我有以下功能:

addCheckbox: function (name, parent, value, text) {
    var div = jQuery("#" + parent);
    var input = jQuery(document.createElement('input')).attr({
        id: "cbid_" + value,
        name: name,
        value: value,
        type: 'checkbox'
    });
    var label = jQuery("<label>").text(text);

    input.appendTo(label);
    div.append(label);
},

Now, this works, but it produces the following result: 现在,这可行,但它产生以下结果:

文本出现在复选框之前

I want to add the checkbox before the label so it would look like [x] Document Files . 我想在标签添加复选框使其看起来像[x] Document Files Is there a way to do this without using html() ? 有没有不使用html()

Yes, you can use prependTo : 是的,你可以使用prependTo

var label = jQuery("<label>").text(text);

input.prependTo(label); // <=== Here
div.append(label);

或者你只能使用CSS:在<input>元素上使用类似于float: left的属性float: left ;或者在<label>元素上使用float: right

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

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