在jquery插件中获取初始选择器

eek*_*eek 19 javascript jquery

我之前得到了一些关于选择器的帮助,但我坚持以下.

假设你有一个像这样的插件

$('#box').customplugin();
Run Code Online (Sandbox Code Playgroud)

如何将#box作为插件中的字符串?不确定这是否是正确的做法,任何其他解决方案也会很棒.

考虑#box是一个选择下拉列表,

我遇到的问题是如果我做常规的JavaScript

$('#box').val(x);
Run Code Online (Sandbox Code Playgroud)

选择了正确的选项值,

但如果我在插件中尝试相同的内容

.....
this.each(function() {
var $this = $(this);


$this.val(x);
Run Code Online (Sandbox Code Playgroud)

最后一个代码并没有真正做任何事情.

我注意到我在插件中定位#box时遇到了麻烦,因为它是一个对象,而不是一个字符串......

任何帮助,将不胜感激.

谢谢!

编辑::放入我正在努力的代码以便更好地理解

(function($){
$.fn.customSelect = function(options) {
    var defaults = {
        myClass : 'mySelect'
    };
    var settings = $.extend({}, defaults, options);


    this.each(function() {
        // Var          
        var $this = $(this);
        var thisOpts = $('option',$this);
        var thisSelected = $this[0].selectedIndex;
        var options_clone = '';

        $this.hide();

        options_clone += '<li rel=""><span>'+thisOpts[thisSelected].text+'</span><ul>'
        for (var index in thisOpts) {
            //Check to see if option has any text, and that the value is not undefined
            if(thisOpts[index].text && thisOpts[index].value != undefined) {
                options_clone += '<li rel="' + thisOpts[index].value + '"><span>' + thisOpts[index].text + '</span></li>'
            }
        }
        options_clone += '</ul></li>';

        var mySelect = $('<ul class="' + settings.myClass + '">').html(options_clone); //Insert Clone Options into Container UL
        $this.after(mySelect); //Insert Clone after Original

        var selectWidth = $this.next('ul').find('ul').outerWidth(); //Get width of dropdown before hiding
        $this.next('ul').find('ul').hide(); //Hide dropdown portion

        $this.next('ul').css('width',selectWidth);

        //on click, show dropdown
        $this.next('ul').find('span').first().click(function(){
            $this.next('ul').find('ul').toggle();
        });

        //on click, change top value, select hidden form, close dropdown
        $this.next('ul').find('ul span').click(function(){
            $(this).closest('ul').children().removeClass('selected');
            $(this).parent().addClass("selected");
            selection = $(this).parent().attr('rel');
            selectedText = $(this).text();
            $(this).closest('ul').prev().html(selectedText);
            $this.val(selection); //This is what i can't get to work
            $(this).closest('ul').hide();
        });

    });
    // returns the jQuery object to allow for chainability.
    return this;
}
Run Code Online (Sandbox Code Playgroud)

BGe*_*sen 18

**编辑**克雷格打败我**/编辑**

**编辑**只是一个单挑:.selector()在jQuery 1.7中被弃用,并在jQuery 1.9中删除:api.jquery.com/selector. - Simon Steinberger**/编辑**

.selector在jQuery集合上使用该属性.

var x = $( "#box" );
alert( x.selector ); // #box
Run Code Online (Sandbox Code Playgroud)

在你的插件中:

$.fn.somePlugin = function() {

    alert( this.selector ); // alerts current selector (#box )

    var $this = $( this );

    // will be undefined since it's a new jQuery collection
    // that has not been queried from the DOM.
    // In other words, the new jQuery object does not copy .selector
    alert( $this.selector );
}
Run Code Online (Sandbox Code Playgroud)

然而,以下可能解决了您真正的问题?

$.fn.customPlugin = function() {
    // .val() already performs an .each internally, most jQuery methods do.
    // replace x with real value.
    this.val(x);
}

$("#box").customPlugin();
Run Code Online (Sandbox Code Playgroud)


Cra*_*aig 5

这个页面讨论了获取选择器:

http://api.jquery.com/selector/