gpt4 book ai didi

javascript - 当它有一个特定的类时,项目不会追加

转载 作者:可可西里 更新时间:2023-11-01 14:45:20 26 4
gpt4 key购买 nike

所以我试图 append 一个选择,当这个插件的类 bootstrap-select 没有 append 时它工作正常:

var select = $("<select class='new-select'></select>");
$('#selects-container').append(select);

但是,当我添加类“selectpicker”时,选择被 append ,但它没有通过插件的 javascript 处理,因此不可见。

var select = $("<select class='new-select selectpicker'></select>");
$('#selects-container').append(select);

所以我的问题是如何让新的 append 选择通过插件的 javascript 运行,并像屏幕加载时出现的选择一样格式化。

衷心感谢您的帮助,不胜感激。

最佳答案

所以有很多方法可以使用 jQuery 声明新的 DOM 节点。具体对于您的任务,您需要指定一个选项对象作为第二个参数,然后向其添加属性,而不是在选择字符串本身中。

试试这个:

var select = $('<select></select>', {
'class': 'new-select selectpicker'
});
$('#selects-container').append(select);

此外,请注意这一点,来自 the docs :

The name "class" must be quoted in the object since it is a JavaScript reserved word, and "className" cannot be used since it refers to the DOM property, not the attribute.

关于javascript - 当它有一个特定的类时,项目不会追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com