javascript - 如何在 JavaScript 中正确使用引号?

标签 javascript html quotation-marks

在我的 JavaScript 代码中,我有以下行:

document.write("</form><button style='margin-top:100px; width:150px; position:absolute; left:50%; margin-left:-75px' type='button' onclick='Proceed('test')'>Se resultat</button>");

样式和类型属性工作正常,但当我尝试将参数传递给 JavaScript 函数 Proceed 时,它不起作用。我也尝试过 (\'test\')

如何解决这个问题?

编辑:完整的 html 脚本

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Title here.</title>
    <script>
        function Proceed(var test)
        {
            alert(test);
        }
    </script>
</head>
<body style="font-family:Palatino">
    <img src="logo.png"/>
    <hr/>
    <h1 style="text-align:center">Description here.</h1>        

    <script>

        if (window.XMLHttpRequest)
        {
            request=new XMLHttpRequest();
        }
        else
        {
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
        request.open("GET","data.xml",false);
        var XMLdocument;
        request.onreadystatechange=function()
        {
            if (request.readyState==4)
            {
                XMLdocument = request.responseXML;
            }
        };
        request.send();
        var questions = XMLdocument.getElementsByTagName("question");
        for(i = 0; i < questions.length; i++)
        {
            var x = questions[i];
            var questionvalue = x.getElementsByTagName("questionvalue");
            document.write("<p style='margin-top:50px; font-size:20px; font-weight:bold; text-align:center'>" + questionvalue[0].childNodes[0].nodeValue + "</p>");
            var answers = XMLdocument.getElementsByTagName("answer");
            document.write("<form>");
            for(n = 0; n < answers.length; n++)
            {
                y = answers[n];
                var answervalue = y.getElementsByTagName("answervalue");
                document.write("<input style='margin-left:43%; margin-right:20px;' type='radio' name='" + questionvalue[0].childNodes[0].nodeValue + "' value='" + answervalue[0].childNodes[0].nodeValue + "'>" + answervalue[0].childNodes[0].nodeValue + "<br/>");
            }
            document.write("</form><button style='margin-top:100px; width:150px; position:absolute; left:50%; margin-left:-75px' type='button' onclick='Proceed(\"test\")'>Se resultat</button>");
        }
    </script>
</body>
    </html>

最佳答案

它不起作用,因为它是无效的 HTML:

document.write("</form><button ... onclick='Proceed('test')'>...</button>");

将写为:

</form><button ... onclick='Proceed('test')'>...</button>

当它写入 DOM 时,onclick 的引用样式决定了它的结束位置,本质上是使 onclick 的值变为 Proceed(.您需要更改引用字符:

document.write("</form><button ... onclick=\"Proceed('test')\">...</button>");

或者:

document.write("</form><button ... onclick='Proceed(\"test\")'>...</button>");

编辑:参见this plunkr举个简单的例子

关于javascript - 如何在 JavaScript 中正确使用引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18103351/

相关文章:

c - 带有引号的 grep 的 execvp()

java - 在 Java 中添加引号

sas - SAS 中不匹配的引号问题

JavaScript - 从下拉列表中选择一个元素时更改多个 div

javascript - TRACKER : error TRK0005: Failed to locate: "CL.exe". 系统找不到指定的文件

javascript - Node js csurf token 始终无效

javascript - 如何加载Jquery Tiny滚动条

javascript - jQuery .fadeIn(和所有 jQuery)不工作

HTML 设置 Facebook 图像

javascript - livesearchgridpanel 上的分页工具栏出现问题