javascript生成的HTML标记中的Javascript函数 [英] Javascript function in HTML tag generated by javascript

查看:72
本文介绍了javascript生成的HTML标记中的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?

推荐答案

使用& 为此:



我遇到了同样的问题并通过替换使用& quot; ,这比使用 \更安全。

Use &quot; for this:

I have come across the same issue and have fixed it by replacing " with &quot;, which is safer than using \".

<script type="text/javascript">
    var html = '';
    html += '<div><a href="#" onclick="showBlock(&quot;#idTextBlocks&quot;);">Show Text Blocks</a></div>';
</script>

这篇关于javascript生成的HTML标记中的Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆