weixin_33724659 2015-08-14 16:38 采纳率: 0%
浏览 35

字符串中的引号

Good Day. I have an ajax function and return this:

<tr onClick="obtener_articulo('1960|X36-004|X36004|Cheque Horizontal de Bronce 200 WOG  de   1/4 " 125 Gramos|0||0|0|Unidad||0.000|0|0||noimage.png')"><td>X36-004</td><td>Cheque Horizontal de Bronce 200 WOG  de   1/4 " 125 Gramos</td></tr>

Using javascript i will add that value to a tabla (i can do that already). My problem is the character " beacuse javascript give this error: SyntaxError: unterminated string literal. I need the character " in that value. I am getting that value from database.

I tried to replace " with \" but did not work.

  • 写回答

1条回答 默认 最新

  • weixin_33725239 2015-08-14 16:44
    关注

    You'll want to update the server code producing the string to produce valid HTML.

    The content of onclick attributes (like all attributes) is HTML text. People tend to forget that. :-) So if the quotes around the onclick attribute value are ", use &quot; within the value for a ":

    <tr onClick="obtener_articulo('1960|X36-004|X36004|Cheque Horizontal de Bronce 200 WOG  de   1/4 &quot; 125 Gramos|0||0|0|Unidad||0.000|0|0||noimage.png')"><td>X36-004</td><td>Cheque Horizontal de Bronce 200 WOG  de   1/4 &quot; 125 Gramos</td></tr>
    

    It looks weird to see that in what looks like JavaScript code, but think about the layers involved: The parser reads the HTML, building up a string attribute value, interpreting entities along the way; then it passes that interpreted string to the JavaScript engine, which sees ".

    Gratuitous live example:

    <div onclick="alert(&quot;Hi there&quot;);">Click me</div>
    <div onclick="alert('1/2 &quot;');">And me</div>

    </div>
    
    评论

报告相同问题?