javascript - 让javascript准确地在原处写入文本

标签 javascript jquery

是否可以让javascript在javascript代码所在的位置输出html?

例如

<html>
<head>
<title>
</title>
</head>
<body>
<div>header</div>
<div>main

<script type="text/javascript" language="javascript">
// print some html here, maybe <a href="http://www.google.com">google</a>
</script>

</div>
<div>footer</div>

</body>
</html>

最终结果应该是这样的:

<html>
<head>
<title>
</title>
</head>
<body>
<div>header</div>
<div>main<a href="http://www.google.com">google</a></div>
</script>

</div>
<div>footer</div>

</body>
</html>

我知道我可以提供包含的 div 和 id,然后让 javascript 像那样插入 anchor ,但我只是想知道是否可以直接执行此操作,就像在 javascript 所在的位置编写 html ?

最佳答案

为此使用 document.write('YOUR_TEXT')

<script type="text/javascript" language="javascript">
    document.write('<a href="http://www.google.com">google</a>') 
</script>

jsFiddle demo

关于javascript - 让javascript准确地在原处写入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11138999/

相关文章:

javascript - 单击 Angular JS 中的链接时的 API 调用

javascript - 如何用jquery切换改变html?

javascript - jQuery 中的 Rel 属性

javascript - jQuery UI 可拖动位于其他元素后面

jquery - 更改横幅中的背景

jquery - 在附加列表项上添加 jQuery UI 按钮?

javascript - 查找行索引的有效方法?

javascript - 检查元素的特殊值

javascript - 从 Objective-C 代码调用 javascript

javascript - 如何使div可滚动