javascript生成的HTML标记中的Javascript函数 [英] Javascript function in HTML tag generated by javascript
问题描述
可能重复:
在其中创建一个包含javascript的链接
我正在尝试将函数放入HTML标签中,我将通过javascript生成。
I am trying to put a function into HTML tag which I am generating by javascript.
我得到了什么:
<script type="text/javascript">
var html = '';
html += '<div><a href="#" onclick="showBlock("#idTextBlocks");">Show Text Blocks</a></div>';
</script>
showBlock(#idTextBlocks);
是JQuery函数,它获取单击链接后需要显示的组件的ID。
The showBlock("#idTextBlocks");
is the JQuery function which takes the ID of the component which needs to be show once the link is clicked.
function showBlock(id) {
$(id).show('slow');
}
问题在于它无效。它生成为:
The problem is that it is not working. It gets generated into:
<a #idtextblocks");"="" onclick="showBlock(" href="#">Show Text Blocks</a>
我试图将引号更改为'
singe: onclick =showBlock('#idTextBlocks');
但这只会破坏我的所有javascript。
I've tried to change the quotes into '
singe: onclick="showBlock('#idTextBlocks');
but this just breaks all my javascript.
所以问题是如何正确地将函数传递给HTML < a>
标签的 onclick
属性,如果标签本身是用javascript生成的?
So the question is how to correctly pass a function into HTML <a>
tag's onclick
attribute if the tag itself is generated with javascript?
推荐答案
使用&
为此:
我遇到了同样的问题并通过替换$修复了它c $ c>使用
& quot;
,这比使用 \
更安全。
Use "
for this:
I have come across the same issue and have fixed it by replacing "
with "
, which is safer than using \"
.
<script type="text/javascript">
var html = '';
html += '<div><a href="#" onclick="showBlock("#idTextBlocks");">Show Text Blocks</a></div>';
</script>
这篇关于javascript生成的HTML标记中的Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!