在 JavaScript 函数中传递字符串参数

2023-12-30

我试图将字符串传递给 JavaScript 函数。

正如这里提到的——在 onclick 函数中传递字符串参数 https://stackoverflow.com/questions/9643311/pass-a-string-parameter-in-an-onclick-function

我正在使用这个简单的代码-

<!DOCTYPE html>
<html>

    <body>
        <script>
            name = "Mathew";
            document.write("<button id='button' type='button' onclick='myfunction(\''" + name + "'\')'>click</button>")

            function myfunction(name)
            {
                alert(name);
            }
        </script>
    </body>

</html>

但在控制台中它给出了类似的错误Uncaught SyntaxError: Unexpected token }.


将您的代码更改为

document.write("<td width='74'><button id='button' type='button' onclick='myfunction(\""+ name + "\")'>click</button></td>")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 JavaScript 函数中传递字符串参数 的相关文章

随机推荐