我试图将字符串传递给JavaScript函数。
正如这里提到的-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>")

关于javascript - 在JavaScript函数中传递字符串参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31960980/

10-12 14:03
查看更多