Javascript createElement() 在 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4861864/
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-08-23 14:27:25  来源:igfitidea点击:

Javascript createElement() not working in Chrome

javascriptgoogle-chrome

提问by Rekha

Javascript createElement()is not working in Chrome but it works in IE and Firefox fine. Why?

JavascriptcreateElement()在 Chrome 中不起作用,但在 IE 和 Firefox 中运行良好。为什么?

回答by John Louros

It's working perfectly, use this code:

它运行良好,使用此代码:

var catDiv = document.createElement("div");
catDiv.innerHTML = "Test";
document.body.appendChild(catDiv);

Another working example (if you have an element with Id = myTableBody in your HTML)

另一个工作示例(如果您的 HTML 中有一个 Id = myTableBody 元素)

var appendingTo = document.getElementById("myTableBody");
var tr = document.createElement("tr");
tr.setAttribute("name", "i");
appendingTo.appendChild(tr);

回答by Krishna K

var name = document.createElement("Div" ); 

will work. And later you can add the attributes like

将工作。稍后您可以添加属性,例如

name.colSpan="2";
document.body.appendChild(name);

Note: don't try to use angular brackets like createElement("<div />"). It will not work in Chrome.

注意:不要尝试使用像createElement("<div />"). 它在 Chrome 中不起作用。

Edit: syntax issue in above code fixed. there should be a dot instead of comma.

编辑:修复了上述代码中的语法问题。应该有一个点而不是逗号。

回答by Caio

Beacause your code is messed up, there's nothing wrong with "createElement":

因为你的代码搞砸了,"createElement" 没有任何问题:

<html>
    <head>
        <meta charset = "utf-8">

        <title></title>

        <script>
            window.onload = function () {
                for (var i = 0; i < 100; i ++) {
                    var div = document.createElement ("div");
                        div.style.border = "1px solid black";
                        div.style.margin = "20px";
                        div.style.padding = "10px";

                    document.body.appendChild (div);
                }
            }
        </script>

        <style></style>
    </head>

    <body></body>
</html>

enter image description here

在此处输入图片说明

回答by SilentAssassin

Consult my answer on thisquestion. I had a similar issue. I was creating new input boxes using createElement and had the same issue.

参考我对这个问题的回答。我有一个类似的问题。我正在使用 createElement 创建新的输入框并且遇到了同样的问题。

回答by lonnykight

So I also couldn't get createElement()to work in chrome. After reading Caio'spost and testing the code provided I figured out what I was doing wrong.

所以我也无法createElement()在 chrome 中工作。在阅读了Caio 的帖子并测试了代码后,我发现了我做错了什么。

On w3schools.com the examples they provide always use the tag namein all caps ex. createElement("DIV"), which is the way I was using it and with poor results.

在 w3schools.com 上,他们提供的示例总是使用所有大写的标签名称,例如。createElement("DIV"),这是我使用它的方式,但效果不佳。

After changing from "DIV"to "div"in my own code it instantly worked.

在我自己的代码中从"DIV"到更改后,"div"它立即起作用了。

Thanks Caio.

谢谢蔡欧。