简体   繁体   English

document.write()如何影响jQuery的功能?

[英]How does document.write() influence the functionality of jQuery?

In a client which uses jQuery and jqxWidget jqxMenu sometimes (approx. 3 of 10 times) fails on only one particular page to be 'enriched' with the appropriate CSS styles (in order to make it a jqxMenu). 在使用jQueryjqxWidget的客户端中,有时jqxMenu(大约10次中的3次)仅在一个特定页面上失败,无法使用适当的CSS样式“丰富”(以使其成为jqxMenu)。

The included code fragments for the jqxMenu works on each other page of the client. jqxMenu包含的代码片段可在客户端的每个其他页面上使用。 The only difference to the working pages is that the failing page also includes Javascript from another server to dynamically display options. 与工作页面的唯一区别是,失败的页面还包含来自另一台服务器的Javascript以动态显示选项。

The remotely included code looks like: 远程包含的代码如下所示:

document.write('<table cellspacing="2" cellspacing="2" border="0" align="center">');
document.write('<tr>');
document.write('<td><a href="http://www.someurl.com" target="_blank"><img src="https://someimage.com/jsp/icons/someimage.gif" border="0" 
...
document.write('</tr>');
document.write('</table>');

When I embed the generated table statically in my code the issue doesn't occur. 当我将生成的表静态嵌入到我的代码中时,不会发生此问题。

Static variant which fixes the issue: 解决此问题的静态变体:

<table cellspacing="2" cellspacing="2" border="0" align="center">
<tr>
<td><a href="http://www.someurl.com" target="_blank"><img src="https://someimage.com/jsp/icons/someimage.gif" border="0" 
...
</tr>
</table>

The question is: Is there a problem using document.write() in conjunction with jQuery and/or jqWidgets? 问题是:将document.write()jQuery和/或jqWidgets结合使用是否存在问题?

What could be a workaround (thinking of wrapper, since parsing the and use jQuery to put them seems to be overkill)? 有什么解决方法(想想包装器,因为解析并使用jQuery放置它们似乎过大了)?

document.write blocks the execution of jQuery code since jQuery acts on elements already in the DOM, while as using document.write, these elements don't exist. document.write阻止jQuery代码的执行,因为jQuery对DOM中已经存在的元素起作用,而使用document.write时,这些元素不存在。 so jQuery is actually unable to find your DOM elements. 因此jQuery实际上无法找到您的DOM元素。

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

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