<script type="text/javascript" src="jquery.js"></script>
<script>
var printLinks = function () {
var links = $(.info).code;
document.write(links);
};
</script>
<form>
<input type="button" value="printlinks" onclick="printLinks()"></input>
</input>
</form>
我正在尝试将特定元素类型中的所有文本写入文档,该元素类型是我按类 $(.info)
查询的元素的子元素。 。我知道 document.write 不是写入文档的最佳方法。 info 是 <code>
的父元素的类包含我要打印的链接的标签。我对 jQuery 很陌生,所以我可能误用了它。任何帮助将不胜感激。
最佳答案
好吧,如果我理解正确的话,你想用 info
类来获取元素中的内容。如果这是正确的,您需要采取以下方法:
<script type="text/javascript">
function printLinks() {
var content = $('.info').html(); // Grab innerHTML of element
$('#idOfTargetElement').html( content ); // write the content here
}
</script>
编辑:
请参阅此 fiddle 以进行说明:
关于javascript - 使用jquery从文档的其他部分写入信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9189111/