无限期地在单引号和双引号之间切换 [英] Alternating between single and double quotes indefinitely

查看:99
本文介绍了无限期地在单引号和双引号之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理多个嵌入式/备用引号时遇到问题,希望有人可以提供帮助.这是相关的代码...

I'm having an issue dealing with multiple embedded/alternating quotes that I'm hoping someone can help with. Here's the relevant code...

displayMessage(

    "post success!", 

    "<p style='margin-top: 15px; margin-bottom: 15px'>if you'd like to share your post...</p> 

    <a onclick='openSharePopup("+textA+","+numberA+","+numberB+")' style='cursor: pointer; font-size:16px'>
        share
    </a>"

);

function displayMessage(heading, text){
  $("#displayMessageHeadingDiv").html(heading);
  $("#displayMessageBodyDiv").html(text);
  $("#displayMessageParentDiv").css("display","block");
}

displayMessage()是在用户成功在网站上发布某些内容后调用的,并且除非"textA"包含撇号(例如类似"Mike's Opinion"之类的东西),否则它可以正常工作

displayMessage() is called after the user successfully posts something on the website, and works perfectly unless "textA" contains an apostrophe (e.g. something like "Mike's Opinion")

问题是(我认为)是,尽管textA封装在双引号内-这些双引号已经在我的onclick属性的单引号内-该本身在displayMessage的整个第二个参数的更多双引号内.

The problem being (I think) is that although textA is encapsulated inside double quotes - those double quotes are already inside the single quotes of my onclick attribute - which itself is inside yet more double quotes of the entire second argument of displayMessage.

我以前认为单引号和双引号之间的交替可以无限期地继续,但是此示例似乎暗示了其他情况.我还尝试了这些替代方法,以破坏textA中的所有引号,但都无效...

I thought previously that alternating between single and double quotes could continue indefinitely but this example seems to suggest otherwise. I've also experimented with these alternatives to break any quotes in textA but neither have worked...

textA = JSON.stringify(textA).replace(/&/, "&amp;").replace(/"/g, "&quot;");

textA = textA.replace("'","\'");

关于我要怎么解决的任何想法?

Any ideas on where I'm going wrong with this?

推荐答案

您的方法从根本上是错误的.手动创建内联事件处理程序几乎总是错误的方法.

Your approach is fundamentally wrong. Manually creating inline event handlers is almost always the wrong way to do it.

例如,更好的解决方案是:

A better solution would be for example:

var shareLink = $( '<a>share</a>' )
                    .css({ cursor: 'pointer', 'font-size': '16px' })
                    .click( function() {
                            openSharePopup( textA , numberA, numberB );
                        });

var msg = $( "<p>if you'd like to share your post...</p>" )
              .css({ 'margin-top': '15px', 'margin-bottom': '15px' });

displayMessage( "post success!", shareLink.add( msg ) );

function displayMessage(heading, text){
  $("#displayMessageHeadingDiv").html(heading);
  $("#displayMessageBodyDiv").empty().append(text);
  $("#displayMessageParentDiv").show();
}

基本上,您应该创建元素,通过代码添加单击处理程序,然后将元素直接添加到容器中.这样,您不必考虑要使用哪种报价以及在何处使用报价.

Essentially you should create the elements, add a click handler through the code, and add the elements directly to the container. This way you don't have to think about what kind of quotes to use and where.

(作为另一个风格上的注释,最好使用CSS类而不是嵌入式样式来对元素进行样式设置.)

(As another stylistic side note, it would be much better to use CSS classes to style the elements instead of inline styles.)

这篇关于无限期地在单引号和双引号之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆