简体   繁体   English

document.getElementById找不到qUnit DOM元素

[英]document.getElementById can't find qUnit DOM elements

I'm trying to get a reference to a DOM object created by qUnit, with no luck. 我正在尝试获取由qUnit创建的DOM对象的引用,但是没有运气。 It works just fine with a "home made" DOM element. 与“自制” DOM元素一起使用时效果很好。 I have made a test site to illustrate the problem. 我已经做了一个测试站点来说明问题。 Turn on Firebug or other logging window when visiting the site. 访问该网站时,请打开Firebug或其他日志记录窗口。

This is the code of the website: 这是网站的代码:

 window.onload = function() { var qunitTestrunnerToolbar_element = document.getElementById("qunit-testrunner-toolbar"); console.log("qunitTestrunnerToolbar_element: ", qunitTestrunnerToolbar_element); var test_element = document.getElementById("test_element"); console.log("test_element: ", test_element); }; 
 <!doctype html> <html> <head> <meta charset="utf-8"> <title>Testing 'require' error</title> </head> <body> <p>See console for output</p> <script src="index.js"></script> <script src="http://code.jquery.com/jquery-2.2.0.js"></script> <p id="test_element">Test element</p> </body> </html> 

It won't work like this 这样就行了

I am not talking about qunit but document.getElementById("qunit-testrunner-toolbar"); 我说的不是qunit而是document.getElementById("qunit-testrunner-toolbar"); will return null because there are no element present in this html. 将返回null,因为此html中没有元素。

If you are particularly asking how to get actual id and not null 如果您特别询问如何获取实际ID,而不是null

You may, add your original script file in this html and then var qunitTestrunnerToolbar_element = document.getElementById("qunit-testrunner-toolbar"); 您可以在此html中添加原始脚本文件 ,然后var qunitTestrunnerToolbar_element = document.getElementById("qunit-testrunner-toolbar"); will console it in indexjs or if you can include <iframe> in your test html you can do consoleindexjs 或者如果你可以包含<iframe>在您的测试HTML,你可以做

<iframe src="urlWithinYourDomain.html" style="display:none" id="iframeId"></iframe> and in indexjs <iframe src="urlWithinYourDomain.html" style="display:none" id="iframeId"></iframe>和indexjs

var qunitTestrunnerToolbar_element = document.getElementById('iframeId').contentWindow.document.getElementById('qunit-testrunner-toolbar'); if you like html way. 如果您喜欢html方式。

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

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