2016-10-03 71 views
1

我正在使用jQuery插件tagEditor,它有一个参数,您可以在其中输入占位符;但我不特别想通过JS代码对此进行硬编码,我更愿意通过实​​际字段placeholder属性动态获取值。获取绑定到jQuery插件的元素的属性

我设法使用这个工作:

$('#tags').tagEditor({ 
    placeholder: $('#tags').attr('placeholder'), 
}); 

然而,再次,我宁愿不要硬编码元素在它的情况我添加了一个二次选择得到占位符的ID。

我试图得到它使用$(this)但它似乎不工作(没有价值);例如:

$('#tags').tagEditor({ 
    placeholder: $(this).attr('placeholder'), 
}); 

...产生一个空值。

有没有什么方法可以在没有硬编码的情况下获得我想要的元素的值?

+0

你的代码本身替换占位?什么意思? – Ivan

+0

你是什么意思? '#tags'是我绑定tagEditor的原始_text field_的'id';我试图从原始文本字段中获取'placeholder'属性的值,以便将其设置为tagEditor'placeholder'参数的值。 – Brett

+0

明白了,从未见过tagEditor – Ivan

回答

1

尝试这样:

$('#tags').each(function(){  
    $(this).tagEditor({ placeholder: $(this).attr('placeholder') }); 
}); 

如果你所担心的性能,因为each,这里是一个另类

$.fn.myTagfunc = function(callback){ 
    callback.apply(this); 
    return this; 
}; 

$(selector).myTagfunc(function(){ 
    $(this).tagEditor({placeholder:$(this).attr('placeholder') }); 
}); 
+0

非常好 - 非常感谢! – Brett