我假设数据为<data:post.title/>
是:
This is "post title' that contain special characters
如何使用javascript读取数据而不先将其放入HTML文档中。 ?
通常,先将获取的数据放入HTML文档中:
HTML
<div id="post-title-wrapper">
<data:post.title/>
</div>
JS:
var post_title = document.getElementById("post-title-wrapper").innerHTML; // This work
但是,我只想读取这些数据,而不先将其放入 html 文档中:
仅限 JS:
var post_title = "<data:post.title/>";
// not work, this is because there are special characters in the data.
// the script will be interrupted:
// var post_title = "This is "post title' that contain special characters";
我应该做什么?
最佳答案
使用 ES2015 Template literals您可以使用反引号``(重音)字符而不是双引号或单引号。
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
var post_title = `<data:post.title/>`;
关于javascript - Blogger.com : Can I read <data:post. title/> 包含带有 javascript 的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41650311/