gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:48:20 25 4
gpt4 key购买 nike

我之前得到了一些有关选择器的帮助,但我坚持以下内容。

假设您有一个这样的插件

$('#box').customplugin();

如何在插件中获取 #box 作为字符串?不确定这是否是正确的方法,任何其他解决方案也很好。

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

我遇到的问题是如果我执行常规的 javascript

$('#box').val(x);

选择正确的选项值,

但是如果我在插件中尝试同样的事情

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


$this.val(x);

最后的代码实际上没有做任何事情。

我注意到我在插件内定位 #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;
}

最佳答案

Just a heads-up: .selector() is deprecated in jQuery 1.7 and removed in jQuery 1.9: api.jquery.com/selector.– Simon Steinberger

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

Note: This API has been removed in jQuery 3.0. The property was never a reliable indicator of the selector that could be used to obtain the set of elements currently contained in the jQuery set where it was a property, since subsequent traversal methods may have changed the set. Plugins that need to use a selector string within their plugin can require it as a parameter of the method. For example, a "foo" plugin could be written as $.fn.foo = function( selector, options ) { /* plugin code goes here */ };, and the person using the plugin would write $( "div.bar" ).foo( "div.bar", {dog: "bark"} ); with the "div.bar" selector repeated as the first argument of .foo().

var x = $( "#box" );
alert( x.selector ); // #box

在你的插件中:

$.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 );
}

但是以下内容可能解决了您真正的问题?

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

$("#box").customPlugin();

关于javascript - 在 jquery 插件中获取初始选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5477394/

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