简体   繁体   English

如何防止jQuery插件更改DOM?

[英]How can I prevent a jQuery plugin from changing my DOM?

I'm using a third-party commenting plugin and customizing a couple of elements using simple jQuery. 我正在使用第三方评论插件,并使用简单的jQuery自定义了几个元素。 In certain circumstances, though, the plugin refreshes itself and wipes out all of the changes I made to it. 不过,在某些情况下,该插件会刷新自身并清除我对其所做的所有更改。 There is no event to attach a handler to associated with this refresh call, and I don't always have control over when it occurs. 没有事件将处理程序附加到与此刷新调用相关联的事件,而且我并不总是能够控制何时发生。

Is there some way to prevent changes to certain elements? 有什么方法可以防止对某些元素进行更改? Or maybe a way to detect if certain (non-form) elements have changed so that I can change them back? 或者,也许是一种方法来检测某些(非形式)元素是否已更改,以便我可以将其更改回?

There is no way to prevent a piece of javascript in the page from changing your DOM elements. 无法阻止页面中的一段JavaScript更改DOM元素。

Your choices are: 您的选择是:

  1. Study the offending javascript and deactivate it (render it so it doesn't operate). 研究有问题的javascript并将其停用(将其渲染以使其无法运行)。 How exactly you do that will depend upon how that javascript works. 具体如何执行将取决于JavaScript的工作方式。 You might replace some functions with more innocuous ones or deregister event handlers after it initializes itself or kill timers it has, etc... 您可以将一些函数替换为更多无害的函数,或者在初始化自身或杀死具有的计时器后注销事件处理程序,等等。

  2. Change the DOM so that the plug-in doesn't target the elements you want it to leave alone. 更改DOM,以使该插件不会定位到您希望其单独保留的元素。 Again, this would depend upon how the plug-in was identifying which elements it would change. 同样,这将取决于插件如何识别它将更改的元素。

  3. Regularly query your affected objects and restore their state after the plugin messes with it. 定期查询受影响的对象,并在插件弄乱之后恢复它们的状态。

  4. Deinstall the plugin. 卸载插件。

Without studying the plug-in code, it is hard for us to be more specific than this. 如果不研究插件代码,我们很难比这更具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM