javascript 我可以在我的自定义 Polymer 元素中使用 jquery 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32469266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:20:04  来源:igfitidea点击:

Can I use jquery within my custom Polymer elements?

javascriptjquerydompolymershadow-dom

提问by Luke

The Polymer documentation says:

聚合物文档说:

Polymer provides a custom API for manipulating DOM such that local DOM and light DOM trees are properly maintained. These methods and properties have the same signatures as their standard DOM equivalents, except that properties and methods that return a list of nodes return an Array, not a NodeList.

Note: All DOM manipulation must use this API, as opposed to DOM API directly on nodes.

Polymer 提供了用于操作 DOM 的自定义 API,以便正确维护本地 DOM 和轻量 DOM 树。这些方法和属性与它们的标准 DOM 等价物具有相同的签名,除了返回节点列表的属性和方法返回一个数组,而不是一个 NodeList。

注意:所有 DOM 操作都必须使用此 API,而不是直接在节点上使用 DOM API。

Jquery has a lotof useful methods though, which make my life easier, like toggleClass, hasClass, addClass, one...

不过,Jquery 有很多有用的方法,它们使我的生活更轻松,例如toggleClass, hasClass, addClass, one...

I have been able to use some of these methods without consequence in my elements, but I am wondering what exactly the semantics are behind the bold part of the above quote, so I can reason about things more clearly.

我已经能够在我的元素中使用其中一些方法而不会产生影响,但我想知道上面引用的粗体部分背后的语义到底是什么,所以我可以更清楚地推理事情。

Note: I'm not talking about using jquery in the main document, I am talking about using it when I am insidethe shadow boundary, like in an element's readycallback for instance.

注意:我不是在谈论在主文档中使用 jquery,而是在我处于阴影边界时使用它,例如在元素的ready回调中。

回答by Neil John Ramal

You can, actually. You'll just have to use Polymer.dom(this.root).querySelectorand wrap that returned element in jquery.

你可以,实际上。您只需要Polymer.dom(this.root).querySelector在 jquery 中使用和包装返回的元素。

Here's an example. http://jsbin.com/purudu/edit?html,output

这是一个例子。http://jsbin.com/purudu/edit?html,输出