【问题标题】:Getting the element that the plugin was binded to in jQuery?在 jQuery 中获取插件绑定到的元素?
【发布时间】:2015-03-22 17:14:05
【问题描述】:

我一直在制作我的第一个 jQuery 插件,我需要通过样式标签在 head 中添加一些 CSS,因为在脚本注入的元素上执行内联 CSS 在 IE8 中有一些奇怪的错误 - 10.

但要在头部添加样式,我需要定位用户将插件绑定到的实际元素。

例如:

$('input').myPlugin(); // I need to get the text "input"
$('#form .foo').myPlugin(); // I need to get the text "#form .foo"

是否可以在插件中获取这些数据?

【问题讨论】:

  • 如果你需要它,你可以走上父母并创建选择器字符串......在这里查看我的解决方案stackoverflow.com/a/28881979/1175966
  • 为什么不为插件特有的特定类创建 CSS,然后在为它们初始化插件时将该类添加到元素中?
  • @AnthonyGrist 嗯,我想这是一个可能的想法。谢谢!
  • @charlietfl 感谢您的链接!

标签: javascript jquery jquery-plugins


【解决方案1】:

$.myPlugin

解决问题的推荐方法是使用this 和下面描述的其他方法。如果您需要选择器作为字符串,您可以将函数定义为 jQuery ($) 的子函数,并以类似于 jQuery 的 &.ajax 和 $.extend 的方式执行该函数。这样做是让它成为 jQuery 的子代和 $.fn 的子代,因此它仍然可以“分类”为常规 jQuery 插件或 jQuery 的原型。当您将插件设置为$.fn 时,它被设置为jQuery 的原型,因此您必须运行jQuery 函数。这里我们这样做,但是因为函数也是对象,我们可以将该函数设置为 $.myPlugin 使其仍然是 jQuery 的一部分,但会阻止用户两次输入选择器。由于this.selector 已弃用(它可能很快会被删除),它比通过函数运行它来生成选择器要好。

$.myPlugin = $.fn.myPlugin = function (selector) {
    console.log(selector);
}

$.myPlugin('#selector'); //Logs: '#selector

$.fn 是完全可选的,以防万一用户忘记使用$.myPlugin(selector) 执行该功能,使用$().myPlugin(selector) 时该功能仍然有效

其他选择

如果您不需要字符串,我建议您使用 jQuery 的 .filter() 或类似函数 (here) 以及 $(this)

$.fn.myPlugin = function () {
    var $this = this;
    var filteredElements = $this.children('a');//

    console.log(filteredElements);
} 
$('#form .class').myPlugin();
//Logs: children that are <a> of #form .class

【讨论】:

  • 是的,我知道$(this),但我确实需要字符串。也不想依赖提供它的用户,希望有一种简单的方法来获取它。
  • @Brett 您也可以要求用户将其作为$.fn 的函数提供为$.fn.myPlugin('#selector')
  • 你的意思是不要像$('#selector').myPlugin()那样称呼它吗?
  • @Brett 是的。你也可以在你的代码末尾做$.myPlugin = $.fn.myPlugin 所以它只是 $.myPlugin
  • 好酷 - 也许您可以使用此解决方案更新您的答案?
【解决方案2】:

该字符串是对象的选择器属性:

$.fn.myPlugin = function(){
    console.log(this.selector);
}
$('#form .foo').myPlugin(); // logs '#form .foo'

但是使用它看起来是一种不好的做法(而且它也已被弃用,正如 charlietfl 指出的那样,有充分的理由)。你不应该需要它。要设置元素的样式,您可以直接做

$.fn.myPlugin = function(){
    this.css('color', 'red');
}

【讨论】:

  • .selector 已弃用...从 1.9 开始也已删除
  • 这就是我设置内联 css 的方法,但正如我所说,当它们设置为 box-sizing: border-box; 时,它会在某些 IE 版本中导致一些关于填充文本字段/文本区域的奇怪行为。如果你手动设置填充它工作正常。
  • @charlietfl 显然仍然可以访问stackoverflow.com/questions/14964760/… ?见github.com/jquery/jquery/blob/…;试试,console,同一页jQuery().jquery; $("body").selector;
  • @guest271314 是的,但是当文档说它已被弃用时,您不能指望它在未来的版本中始终可用。我只是引用文档中所说的内容
【解决方案3】:

试试

$.fn.myPlugin = function(sel) {
  console.log(sel, $(this));
};
var selectors = ["input", "#form .foo"];
$(selectors[0]).myPlugin(selectors[0]); // I need to get the text "input"
$(selectors[1]).myPlugin(selectors[1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

<input type="text" />
<form id="form">
  <select class="foo" name="foo">
    <option value="abc">abc</option>
  </select>
</form>

【讨论】:

  • 谢谢....但得到了太多回报。对于input,我得到了返回:["input#.form-control", "input#.form-control", "input#.form-control", "input#.form-control", "input#."]
  • 没有改变我得到的输出;虽然我的测试场景是在多个输入字段上。我检查了你上面的控制台和第一个输出input,但第二个输出select.foo而不是#form .foo
  • 感谢您的努力,但根据我对@vihan1086 的评论,我真的不想让用户将其作为参数提供。
  • 试过@dystroy 的回答?尽管 .selector 记录为 deprecated ,但似乎仍然可以访问;见$.fn.initgithub.com/jquery/jquery/blob/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
相关资源
最近更新 更多