admin管理员组文章数量:1419611
Here's an example of what I'm trying to edit:
<script id="login-popup" type="text/template">
<h3 id="cover-msg" class="modal-title">You need to login to do that.</h3>`
</script>
I would like to add: class="title"
to the h3 tag. This is being done via a chrome extension, so I can't control the HTML that is rendered.
Here's the caveat: I can't assume that the template will always be the same, so I can't just replace or edit the entire thing. I need to be able to select certain elements within the text and only add things as needed.
The problem I'm having is that the template seems to just be plain text. So I can't select it with something like #login-popup #cover-msg
. Please correct me if I'm wrong.
Is it possible to do this with JavaScript/jQuery?
Here's an example of what I'm trying to edit:
<script id="login-popup" type="text/template">
<h3 id="cover-msg" class="modal-title">You need to login to do that.</h3>`
</script>
I would like to add: class="title"
to the h3 tag. This is being done via a chrome extension, so I can't control the HTML that is rendered.
Here's the caveat: I can't assume that the template will always be the same, so I can't just replace or edit the entire thing. I need to be able to select certain elements within the text and only add things as needed.
The problem I'm having is that the template seems to just be plain text. So I can't select it with something like #login-popup #cover-msg
. Please correct me if I'm wrong.
Is it possible to do this with JavaScript/jQuery?
Share Improve this question edited Jul 23, 2015 at 22:26 albert 8,1533 gold badges48 silver badges64 bronze badges asked Jul 23, 2015 at 22:01 JustinJustin 2,3344 gold badges17 silver badges23 bronze badges 4- $("#cover-msg").addClass('title'), or are you trying to change the type of the script? I think you can do $("#login-popup").attr('type','text/sometemplatevariable') – Bryan Mudge Commented Jul 23, 2015 at 22:04
- 2 @BryanMudge The tag isn't rendered as a part of the DOM because it's in a script tag. – Etheryte Commented Jul 23, 2015 at 22:05
-
2
You'll have to get the
innerHTML
of the script block, parse it, do whatever you need to do, and then set theinnerHTML
from the result. – Pointy Commented Jul 23, 2015 at 22:06 - you could use a template library like trimpath, handlebars, etc. to render the text to html and then manipulate it as you wish – Stephen Thomas Commented Jul 23, 2015 at 22:11
1 Answer
Reset to default 7You can follow this type of procedure which gets the text out of the script tag, inserts it into a DOM element so you can use DOM manipulation on it, then gets the resulting HTML out of that DOM element. This allows you to avoid any manual parsing of the HTML text yourself:
var t = document.getElementById("login-popup");
var div = document.createElement("div");
div.innerHTML = t.innerHTML;
$(div).find("h3").addClass("title");
t.innerHTML = div.innerHTML;
It follows this process:
- Get the innerHTML from the script tag
- Create a temporary div
- Puts the HTML into the temporary div where you can then treat it as DOM elements
- Using DOM query, find the
<h3>
- Adds the class to it
- Get the HTML back out of the temporary div
- Puts the HTML back into the script tag as the modified version of the template.
It works here: http://jsfiddle/jfriend00/mqnf1mmp/.
本文标签:
版权声明:本文标题:jquery - Is it possible to edit the contents of a <script type="texttemplate"> with JavaScript? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745316019a2653158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论