這是不可能的; 瀏覽器不會將<script>
標記內的HTML內容視為DOM的一部分。 當您使用$('#idhere').html()
檢索<script>
標記的內容時,您將獲得字符串結果。
為了回答Troy的問題,他最有可能在他的文檔的<head>
中包含模板,這樣他最終可以在瀏覽器端動態呈現內容。 但是,如果是這種情況,OP應使用與text/html
不同的MIME類型。 您應該使用未知的MIME類型,例如text/templates
使用text/html
混淆內容的用途。
我猜你試圖進入<script>
標簽並獲取div
的原因是因為你在單個<script>
標簽中構建了較小的子模板。 那些較小的模板應該放在它們自己的<script></script>
標簽中,而不是包含在一個大的<script></script>
標簽對中。
所以,而不是:
<script type="text/template" id="big_template">
<div id="sub_template_1">
<span>hello world 1!</span>
</div>
<div id="sub_template_2">
<span>hello world 2!</span>
</div>
</script>
做這個:
<script type="text/template" id="template_1">
<span>hello world 1!</span>
</script>
<script type="text/template" id="template_2">
<span>hello world 2!</span>
</script>