我有一堆对象返回一个DOM树,然后我用它来附加到一个元素.例如,
var elem = document.createElement('div');
elem.appendChild(callSomePrototypesMethod());
elem.appendChild(callSomePrototypesMethod());
elem.appendChild(callSomePrototypesMethod());
Run Code Online (Sandbox Code Playgroud)
我最终会得到类似的结果:
<div>
<div id="...">...</div>
<div id="...">...</div>
<div id="...">...</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这三个DIV是由第三方代码创建的,我在这里将它们包装在我的DIV标签周围,但我想要的只是将它们附加到一个空元素,但它不起作用:
var elem = document.createElement('');
elem.appendChild(callSomePrototypesMethod());
elem.appendChild(callSomePrototypesMethod());
elem.appendChild(callSomePrototypesMethod());
Run Code Online (Sandbox Code Playgroud)
预期成绩:
<div id="...">...</div>
<div id="...">...</div>
<div id="...">...</div>
Run Code Online (Sandbox Code Playgroud)
我不确定这对你有什么意义,但我需要使用DOM,我不能把这些内容放在任何其他元素中......
XML
<bookstore xmlns="http://www.contoso.com/books"
xmlns:g="http://www.contoso.com/genre">
<book g:genre="novel" publicationdate="2010-03-01" ISBN="1-123456-15-0">
<title>61 Hours</title>
<author xmlns="http://www.contoso.com/author">
<first-name>Lee</first-name>
<last-name>Child</last-name>
</author>
<price>6.99</price>
</book>
<bookstore>
Run Code Online (Sandbox Code Playgroud)
我需要添加一个书籍节点..我的代码是这样的
strpath = "C:\\BookStore.xml";
XmlDocument doc = new XmlDocument();
doc.Load(strpath);
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("b", "http://www.contoso.com/books");
nsMgr.AddNamespace("g", "http://www.contoso.com/genre");
nsMgr.AddNamespace("a", "http://www.contoso.com/author");
// Create a Book element and populate its attributes
System.Xml.XmlElement XmlElementbook = doc.CreateElement("book");
//create the three attributes to hold the values
XmlElementbook.SetAttribute("g:genre";"novel5");
XmlElementbook.SetAttribute("publicationdate", "2010-11-03");
XmlElementbook.SetAttribute("ISBN", "1-00000-00-00");
// Insert the new element into the XML tree
// Create …Run Code Online (Sandbox Code Playgroud) 首先:我没有受过javascript的教育.所以有一个appendChild方法的问题.那是我的代码:
var wrapper = document.getElementsByClassName('innerWrap');
var post = document.createElement('input');
post.style.position = "absolute";
post.style.top = "100px";
document.wrapper.appendChild(post);
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
提前致谢!
好吧,在这里我继续,尝试学习这个非常好的语言...所以我以前有一个非常丑陋的代码充满'document.write()'和更丑陋的东西,现在我正在将它转换成一个非常好的标准兼容代码,我喜欢它!但我发现一个问题,我没有看到逻辑.在这里:
在html文件中我有这个:
<body onload="generatetable(0)">
Run Code Online (Sandbox Code Playgroud)
在.js文件中我有这个:
function generatetable(product) {
var tbinfo = document.createElement("table"); //table
var tbbody = document.createElement("tbody"); //tbody
var row = document.createElement("tr"); // creates row
for (var i = 0; i < 4; i++) { // creates 4 cells
var cell = document.createElement("td");
}
var tname = arraypro[product].Name;
cell.appendChild(tname);
Run Code Online (Sandbox Code Playgroud)
(我不粘贴表的其余部分,因为它似乎工作正常)
在运行时,我var tname = arraypro[product].Name在线路上出现"异常8"错误
但如果我只做一个
alert(arraypro[0].Name);
Run Code Online (Sandbox Code Playgroud)
它输出的确是输出的,非常正确的单词.如何arraypro[product].Name通过alert(如果你传递'product'的值)而不是通过appendChild?来检索值?
我仍然不太熟悉编程逻辑,但我尝试......
PD:arraypro信息所在的地方已被宣布为全球价值,一切都可以获取,万一重要的是要知道.
非常感谢这里的任何输入.
这是初学者的问题......
我想写一些代码来初始化我的网页中的一堆座位图片,
为它们创建和设置属性,并使它们每9个座位更换一行(4行,每行有9个座位),这里是代码
function initSeats() {
var seatsDiv = document.getElementById("seats");
//Initialize the appearence of all seats
for (var i = 0; i < seats.length; i++) {
for (var j = 0; j < seats[i].length; j++) {
var currentSeatIndex = i * seats[i].length + j;
if (seats[i][j]) {
//if current seat is available(true), create a new IMG element and set some attributes;
} else {
//if current seat is unavailable(true), create a new IMG element and set some attributes;
}
} …Run Code Online (Sandbox Code Playgroud) 我有这块
var inviteSection = document.getElementByClassName("invitedFriends");
var friend = document.createElement("div").class = "friend";
var mail = document.createElement("p").class = "mail";
var span = document.createElement("span").class = "status accepted";
function main() {
inviteSection.appendChild(friend);
friend.appendChild(mail);
friend.appendChild(span);
}
main();
Run Code Online (Sandbox Code Playgroud)
当我运行这个 chrome 控制台时,它说“未捕获的类型错误:inviteSection.appendChild 不是一个函数”是否有任何解决方法请帮助我!
我的HTML中有这些标签,
<div id="main">
<div id="options"></div>
<div id="searchField"></div>
<div id="options"></div>
<div id="results">
***<!-- I want to add elements here -->***
<div id="pagenumber">
<div id="footer">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
什么是在上面id="pagenumber"和里面附加孩子的javascript代码id="results"?
这是一个例子。检查控制台以获取结果。前两个divs(未附加;<script>在控制台上方)具有适当的间距和缩进。但是,div即使它们完全相同,但附加的后两个s 也不会显示与原始相同的格式或空格。
例如输入
var newElem = document.createElement('div');
document.body.appendChild(newElem);
var another = document.createElement('div');
newElem.appendChild(another);
console.log(document.body.innerHTML);
Run Code Online (Sandbox Code Playgroud)
给出输出
<div><div></div></div>
Run Code Online (Sandbox Code Playgroud)
当我想要它看起来像
<div>
<div></div>
</div>
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在附加元素之间生成适当的空白,并在使用innerHTML(或可能的类似方法)获取它时保留该间距?我需要能够直观地显示我正在处理的页面的层次结构和结构。
我尝试将它附加到实际 HTML 中的元素中,但它具有相同的行为
我可以按照 lincolnk 的建议使用文本节点和换行符来执行此操作,但它需要影响动态结果,这意味着我不能使用相同的方法,.createTextNode(' </br>')因为不同的元素位于层次结构的不同级别
请不要使用jQuery
我是初学者,所以我很抱歉这是一个愚蠢的问题.问题是我无法使用JS在div"app"中添加div"title".请帮帮我.
<!DOCTYPE HTML>
<html>
<head>
<meta charset = "utf-8">
<meta name = "description" content = "Field v.1">
<title> Field v.1 </title>
<style>
.app{
background-color: 'black';
}
.title{
background-color: 'green';
}
</style>
</head>
<body>
<div class = "app">
</div>
<script>
var app = document.querySelector('.app')
var title = document.createElement('div')
title.className = 'title'
title.innerHTML = "BREAKING NEWS"
app.appendChild(title);
</script>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
我有以下嵌套字典:
d = {'A':{'a':1}, 'B':{'b':2}}
Run Code Online (Sandbox Code Playgroud)
我想在d不覆盖的情况下添加值。
因此,如果我想附加值 ['A', 'b', 3] 字典应为:
d = {'A':{'a':1, 'b':3}, 'B':{'b':2}}
Run Code Online (Sandbox Code Playgroud)
d['A'].append({'b':3}) 错误:
AttributeError: 'dict' 对象没有属性 'append'
我不知道嵌套字典会提前是什么。所以说:
d['A'] = {'a':1, 'b':3}
Run Code Online (Sandbox Code Playgroud)
不适用于我的情况,因为我在脚本运行时“发现/计算”了这些值。
谢谢
appendchild ×10
javascript ×8
dom ×3
html ×3
append ×2
c# ×1
dictionary ×1
exception ×1
namespaces ×1
python ×1
spacing ×1
xml ×1