Javascript 用js构建锚
我正在尝试使用JavaScript构建以下HTML:Javascript 用js构建锚,javascript,html,dom,Javascript,Html,Dom,我正在尝试使用JavaScript构建以下HTML: <td> <a data-toggle="modal" class="btn" href="sales.asp?origen=NY&number=036529" data-target="#myModal">View Sale</a> </td> 我试图使用以下代码构建它,但出现错误: document.write("<td> <a data-toggle='mo
<td> <a data-toggle="modal" class="btn" href="sales.asp?origen=NY&number=036529" data-target="#myModal">View Sale</a> </td>
我试图使用以下代码构建它,但出现错误:
document.write("<td> <a data-toggle='modal' class='btn' href=.......
document.write(“”)
文件。填写(“”);
}
文件。填写(“”);
在jQuery中,您可以创建指向google的链接,并将其附加到id为“content”的元素中,如下所示:
$("<a/>",{"href":"http://www.google.com"}).text("Google").appendTo("#content");
$(“将普通JavaScript与DOM API结合使用:
//创建所需的元素
var td=document.createElement('td');
var a=document.createElement('a');
//应用属性
a、 setAttribute('data-toggle','modal');
a、 setAttribute('data-target','#myModal');
a、 setAttribute('class','btn');
a、 setAttribute('href','sales.asp?origen=NY&number=036529');
//由于一致性问题,textContent优先于innerText的innerHTML
a、 text内容='查看销售';
//将锚点注入表格单元格
td.儿童(a);
//将所有这些放在DOM中
document.getElementsByTagName('body')[0].appendChild(td);
正如其他人所说,document.write()
将覆盖页面的其余部分。更重要的是,即使它确实有效,它如何知道将表格单元格放在何处
相反:
在文档上创建元素
var td = document.createElement('td');
它还没有被正确地添加到DOM中,但这没关系。接下来我们需要创建内容,即单元格内部的链接。您可以使用与
类似的方法来完成此操作,但将其添加到td
的innerHTML
属性中,无论是在编写还是在代码性能方面,都更有效。像这样:
td.innerHTML = '<a data-toggle="modal" class="btn" href="sales.asp?origen=NY&number=036529" data-target="#myModal">View Sale</a>'
这是我想要的正确方式,因此我创建了表并插入了一个带有链接的按钮,该链接由数组的一个元素组成:
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/xmlBuys.asp?origen=NY",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table class='table table-striped table-condensed table-bordered' id='example'>");
document.write("<thead>");
document.write("<tr class='odd gradeX'>");
document.write("<th>Button</th>");
document.write("</tr>");
document.write(" </thead>");
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td> <a data-toggle='modal' class='btn' href='sales.asp?origen=NY&number="); document.write(x[i].getElementsByTagName("numSale")[0].childNodes[0].nodeValue); document.write("' data-target='#myModal'> View Sale </a> "); document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
</script>
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“xml/xmlbooks.asp?origen=NY”,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
文件。填写(“”);
文件。填写(“”);
文件。填写(“”);
文件。写入(“按钮”);
文件。填写(“”);
文件。填写(“”);
var x=xmlDoc.getElementsByTagName(“项目”);
对于(i=0;我不能使用document.write)。请参见此处:看起来您想要使用DOM(mb i'm Error)如果你在直的JS中不是很好,你应该考虑使用jQuery为DOM。首先,你不能使用文档。写它将用你所写的任何东西来替换你的所有页面。第二,如果可能的话,提供你想要达到的所有代码以得到快速的解决方案。我同意,jQuery不是所有的。但是你的解决方案是与我的1行相比,大约有9行长。你的代码的大小大约是所有jQuery的1/60。也就是说,它确实可以工作。我并不反对jQuery,它是一个很棒的库。只是OP没有提到使用它,所以我不愿意把它推到不需要的地方。我同意@EliranMalka。这里的解决方案可能有9行长,但有inc使用jQuery意味着一个更长的解决方案——只是额外的行被隐藏在另一个文件中,由其他人编写。jQuery很好,但是如果有人不使用jQuery,或者没有提到使用它,要求他们将它添加到脚本中以解决一个问题就不是特别有效。
// `table` should be the table you want to add the cell to
// for example, if your table has the id 'table'...
var table = document.getElementById('table'); // will get table
table.appendChild(td); // will append `td`, your cell and link to the table
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/xmlBuys.asp?origen=NY",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table class='table table-striped table-condensed table-bordered' id='example'>");
document.write("<thead>");
document.write("<tr class='odd gradeX'>");
document.write("<th>Button</th>");
document.write("</tr>");
document.write(" </thead>");
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td> <a data-toggle='modal' class='btn' href='sales.asp?origen=NY&number="); document.write(x[i].getElementsByTagName("numSale")[0].childNodes[0].nodeValue); document.write("' data-target='#myModal'> View Sale </a> "); document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
</script>