最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Need third kind of quotation marks in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since it is produced in a for-loop. In it there is an <a> tag. To the <a> tag I want to add an onClick which calls a function with a parameter which is saved in a javascript variable.

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window("+parameter+")'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

This does not work because it would prodece the following html:

<a href='#' onClick='create_sprite_window(p1)'></a>

It does not work because there are no quotation marks before and after p1. My problem is that I would need a third kind of quotation marks in order to solve this. In the following example I will use # where I would need those quotation marks:

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window(#"+parameter+"#)'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

I can't use a single quotation mark because it would end the onClick and I can't use double beacuse it would end to_html_String.

Are there third quotation marks or is there an other way to solve this?

I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since it is produced in a for-loop. In it there is an <a> tag. To the <a> tag I want to add an onClick which calls a function with a parameter which is saved in a javascript variable.

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window("+parameter+")'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

This does not work because it would prodece the following html:

<a href='#' onClick='create_sprite_window(p1)'></a>

It does not work because there are no quotation marks before and after p1. My problem is that I would need a third kind of quotation marks in order to solve this. In the following example I will use # where I would need those quotation marks:

let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window(#"+parameter+"#)'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

I can't use a single quotation mark because it would end the onClick and I can't use double beacuse it would end to_html_String.

Are there third quotation marks or is there an other way to solve this?

Share Improve this question asked Aug 3, 2016 at 9:19 niklasscniklassc 5502 gold badges13 silver badges29 bronze badges 1
  • 1 you can also use escape char ... \' or \" as appropriate – Jaromanda X Commented Aug 3, 2016 at 9:23
Add a ment  | 

4 Answers 4

Reset to default 5

If you're using ES6, you can use template strings like

let foo = `Hello "World's"`;

Anyway - why wouldn't you just escape your quotation marks ?

let bar = 'hello \' world';
let buz = "hello \" world";

Don’t build HTML using JavaScript. The DOM API will let you build document nodes cleanly and safely, and even attach event listeners so you don’t have to build JavaScript strings in the HTML you’re building in JavaScript.

let parameter = "p1";

const link = document.createElement('a');
link.href = '#';
link.textContent = 'maybe you want something in this link?';
link.addEventListener('click', function (e) {
    e.preventDefault();
    create_sprite_window(parameter);
});

document.getElementById('sidebar_left_sprites')
    .appendChild(link);
  • createElement
  • addEventListener
  • appendChild

This will work

"<a href='#' onClick='create_sprite_window('"+parameter+"')></a>";

/* Use \' to differentiate with function single quote */

let parameter = "p1"; let to_html_String = ""; document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;

发布评论

评论列表(0)

  1. 暂无评论