在paraghaph标题中添加单引号和空格

时间:2015-06-15 04:13:16

标签: javascript html

我在段落中动态添加标题。但单个空格文本未完全添加标题。

  <!docType Html>
    <html>
    <head>
    </head>
    <body>
    <div id="file">
    </div>
    <script>
    var arr= ["bo'i kisahn","kishan"];

    var string="";
    for(var i=0;i<arr.length;i++){
    string =string + "<p title="+arr[i]+">kishan"+i+" </p>";
    }
    document.getElementById('file').innerHTML=string;
    </script>
    </body>
    </html>

2 个答案:

答案 0 :(得分:1)

您需要使用""包装属性值,因为它包含特殊字符

var arr = ["bo'i kisahn", "kishan"];

var string = "";
for (var i = 0; i < arr.length; i++) {
    string = string + "<p title=\"" + arr[i] + "\">kishan" + i + " </p>";
}
document.getElementById('file').innerHTML = string;

var arr = ["bo'i kisahn", "kishan"];

var string = "";
for (var i = 0; i < arr.length; i++) {
  string = string + "<p title=\"" + arr[i] + "\">kishan" + i + " </p>";
}
document.getElementById('file').innerHTML = string;
<div id="file"></div>

答案 1 :(得分:0)

您可以使用REG_EXTENDED来显示单引号。
请检查http://jsfiddle.net/r2m6tkf7/