简体   繁体   English

如何传递包含单引号和双引号的字符串?

[英]How to pass a string that contains single and double quotes?

I have string variable called name .我有一个名为name的字符串变量。 name contains the string 14'6" . I'm trying to pass name to a function. I'm trying to add an onclick to a tablerow this like this: name包含字符串14'6" 。我正在尝试将name传递给 function。我正在尝试将onclick添加到如下表格中:

$("#trID").attr('onclick', 'testFunction(' + name + ')');

I tried using the escape character to achieve this, however I didn't get this to work because the string contains both a single quote ( ' ) and a double quote ( " ).我尝试使用转义字符来实现这一点,但是我没有让它工作,因为字符串包含单引号( ' )和双引号( " )。

Is there a way I can pass thing string in to a function or do I have to change the string completely?有没有办法可以将字符串传递给 function 或者我必须完全更改字符串?

Thanks!谢谢!

To do what you require you need to escape the single/double quotes within the name string.要执行您需要的操作,您需要转义name字符串中的单引号/双引号。 To do that you could use a regex:为此,您可以使用正则表达式:

name = name.replace(/([\""|\''])/g, '\\$1');
$("#trID").attr('onclick', 'testFunction(' + name + ')');

However it should be noted that using inline event handlers, ie.但是应该注意的是,使用内联事件处理程序,即。 the onclick attribute in your example, is not good practice.您示例中的onclick属性不是一个好习惯。 You should use unobtrusive event handlers instead.您应该改用不显眼的事件处理程序。

As you've already included jQuery in the page it would be achieved with an event handler and a data attribute, like this:由于您已经在页面中包含了 jQuery ,因此可以使用事件处理程序和data属性来实现,如下所示:

 let $div = $("#trID").on('click', function() { console.log($(this).data('foo')); }); $('button').on('click', function() { $div.data('foo', (new Date()).getTime()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="trID" data-foo="14'6"">Click me to display value</div> <button>Click me to change value</button>

Try the string template of javascript试试javascript的字符串模板

$("#trID").attr('onclick', `testFunction(${name})`);

You can use JSON as proxy since its array syntax is based on JavaScript and JSON.stringify() can cope with JSON fragments (such a standalone string): You can use JSON as proxy since its array syntax is based on JavaScript and JSON.stringify() can cope with JSON fragments (such a standalone string):

 function testFunction(param){ console.log(param); } var rawName = "one\n\"two\"\n'three'\nfour"; var encodedName = JSON.stringify(rawName); console.log(rawName, encodedName); $("#trID").attr('onclick', 'testFunction(' + encodedName + ')');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="trID">Click Me!</button>

It's worth noting anyway that you don't need it in the first place.无论如何,值得注意的是,您一开始就不需要它。 Dynamically generated code is hard.动态生成的代码很难。 Since you're using jQuery, a cleaner way would be:由于您使用的是 jQuery,因此更清洁的方法是:

 function testFunction(param){ console.log(param); } var name = "one\n\"two\"\n'three'\nfour"; $("#trID").on('click', function() { testFunction(name); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="trID">Click Me!</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM