了解设置JQuery变量 [英] Understanding Setting JQuery Variables

查看:91
本文介绍了了解设置JQuery变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我介绍了设置一个JQuery变量,如下所示,通过我在StackOverflow上的另一个问题上寻找帮助。您可以通过简单地调用变量来创建输入字段,并且看起来锚变量也定义了样式!

Recently I was introduced to setting a JQuery variable as seen below via assistance I was looking for on another question I had on StackOverflow. You can create an input field via simply calling upon a variable and it also appears that the anchor variable defines styles too!

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]));

我相信我理解如何修改和重新创建上面的代码片段,这是我实际上没有的唯一方面理解是风格的设定。根据我的研究,我还没有发现任何类似的方法,我希望通过发帖来找到更多的理解。

I believe I understand how to modify and recreate the above snippet, the only aspect I don't actually understand is the setting of the styles. Upon my research, I haven't yet found anything alike this method and I'm hoping I can find more of an understanding via posting a question.

我的目标是理解并更频繁地使用我的下一个目标是创建一个选择选项字段,其中所有选项最后都要求 .selectBoxIt()

My aim is to understand and use more frequently with my next target being to create a select option field with all the options calling for .selectBoxIt() at the end.

我不完全确定这是否是最佳方式为了达到这个目的,我得到了一个解决方案,感谢答案如何创建一个选择选项列表;

I'm not entirely sure if this is the best way to achieve this however I've come up with a solution thanks to answers to as how to create a select option list;

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/ 并向下滚动到有关创建新元素的部分。

You can start here http://api.jquery.com/jquery/ and scroll down to the section on creating new elements.


从jQuery 1.4开始,jQuery()的第二个参数可以接受一个简单的
对象,该对象包含可以传递的属性的超集$ b $ .b到.attr()方法。

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.

这篇关于了解设置JQuery变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆