了解设置 JQuery 变量
最近我被介绍设置一个 JQuery 变量,如下所示,通过我在 StackOverflow 上遇到的另一个问题寻找的帮助。您可以通过简单地调用一个变量来创建一个输入字段,而且 anchor 变量似乎也定义了样式!
var clicked = $('.clicked');
var ul = clicked.children('ul');
var input = $('<input />', {
type: 'text',
class: 'rename',
value: 'New Folder',
focusout: function() {
$(this).siblings('a').html($(this).val()).show();
$(this).remove();
$(anchor).parent().removeClass('clicked');
}
});
var anchor = $('<a>', {
href: '#',
css: {
display: 'none'
}
});
ul.prepend($('<li>').append([input.select(), anchor]));
我相信我了解如何修改和重新创建上面的代码片段,我真正不了解的唯一方面是样式的设置。根据我的研究,我还没有发现任何类似这种方法的东西,我希望我能通过发布问题找到更多的理解。
我的目标是更频繁地理解和使用我的下一个目标是创建一个选择选项字段,所有选项最后都调用 .selectBoxIt()
。
更新
我不完全确定这是否是实现此目的的最佳方法,但是由于有关如何创建选择选项列表的答案,我想出了一个解决方案;
var select = $('<select />');
var options = [
{val : 1, text: '3 DAYS'},
{val : 2, text: '10 DAYS'},
{val : 3, text: '30 DAYS'},
{val : 4, text: '60 DAYS'}
];
$('.hello').prepend(select);
$(options).each(function() {
select.append($("<option>").attr('value',this.val).text(this.text));
});
// Because we are appending at a later date
// for the .selectBoxIt(); to work...
$(".hello select").selectBoxIt();
最佳答案
您可以从这里开始http://api.jquery.com/jquery/并向下滚动到有关创建新元素的部分。
As of jQuery 1.4, the second argument to jQuery() can accept a plain object consisting of a superset of the properties that can be passed to the .attr() method.
关于javascript - 了解设置 JQuery 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37336026/