document.getElementById无法找到qUnit DOM元素

时间:2016-02-03 21:08:43

标签: javascript html dom qunit

我试图获取qUnit创建的DOM对象的引用,但没有运气。它与#34;自制的#34; DOM元素。我做了test site来说明问题。访问网站时打开Firebug或其他日志记录窗口。

这是网站的代码:



import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
import javax.swing.JComboBox;

public class Window extends JDialog {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            Window dialog = new Window();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public Window() {
        setBounds(100, 100, 450, 153);
        getContentPane().setLayout(new BorderLayout());
        {
            JScrollPane scrollPane = new JScrollPane();
            getContentPane().add(scrollPane, BorderLayout.CENTER);
            {
                JPanel panel = new JPanel();
                scrollPane.setViewportView(panel);
                panel.setLayout(new MigLayout("", "[grow]", "[][][][][][][]"));
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 0,growx");
                }
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 1,growx");
                }
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 2,growx");
                }
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 3,growx");
                }
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 4,growx");
                }
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 5,growx");
                }
                {
                    JComboBox comboBox = new JComboBox();
                    panel.add(comboBox, "cell 0 6,growx");
                }
            }
        }
    }

}

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);
};




1 个答案:

答案 0 :(得分:1)

它不会像这样

我不是在谈论qunit,但是document.getElementById("qunit-testrunner-toolbar");将返回null,因为此html中没有元素。

如果您特别询问如何获得实际ID而不是null

您可以 在此html中添加原始脚本文件 ,然后var qunitTestrunnerToolbar_element = document.getElementById("qunit-testrunner-toolbar");console indexjs 如果您可以在测试html中包含<iframe>,则可以

<iframe src="urlWithinYourDomain.html" style="display:none" id="iframeId"></iframe> 并在indexjs

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