首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML working with jQuery template

HTML working with jQuery template
EN

Stack Overflow用户
提问于 2021-10-12 13:51:05
回答 2查看 49关注 0票数 2

我有一个Shopify liquid我加载到HTML模板

代码语言:javascript
复制
<script type="text/template" id="description">
  <div class="product-ddd">
    {{ product.description }}
  </div>

</script>
<script>
  $("#description").each(function() {
    console.log($(this).html());
  });
</script>

由于某些原因,find无法工作。

代码语言:javascript
复制
<script>
  $("#description").find('h4').each(function() {
    console.log($(this).html());
  });
</script>

这是{{ product.description }}返回的内容

代码语言:javascript
复制
<div class="product-ddd">
  <h4>DETAILS &amp; DIMENSIONS</h4>
  <p>A trendy fashion-forward look has never been so comfortable. This full-length maxi dress for women is a must-have year-round style. Featuring a lovely scoop neckline, elbow-length sleeves, full A-line skirt, ankle-length hemline, and it is made from
    a soft comfortable stretch material. Pair this maxi dress with a denim or leather jacket for a stylishly edgy look. Available in four stylish colors and it is machine washable for easy care. Made in the USA from a comfortable stretch material.
  </p>
  <h4>FABRIC CONTENT &amp; CARE</h4>
</div>

这是最终的脚本模板

代码语言:javascript
复制
<script type="text/template" id="hesdsdllo">
  <div class="product-ddd">
    <h4>DETAILS &amp; DIMENSIONS</h4>
    <p>A trendy fashion-forward look has never been so comfortable. This full-length maxi dress for women is a must-have year-round style. Featuring a lovely scoop neckline, elbow-length sleeves, full A-line skirt, ankle-length hemline, and it is made from
      a soft comfortable stretch material. Pair this maxi dress with a denim or leather jacket for a stylishly edgy look. Available in four stylish colors and it is machine washable for easy care. Made in the USA from a comfortable stretch material.
      <br
        data-mce-fragment="1">&nbsp;</p>
    <h4>FABRIC CONTENT &amp; CARE</h4>
  </div>
</script>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-12 14:03:04

这个问题是因为<script>标签是type="text/template"的。这意味着其中的HTML不会呈现为DOM的一部分,而是作为纯文本处理。

因此,要从其中获取内容,需要解析字符串以从其中创建DOM,然后使用jQuery (或普通JS)方法从其中检索所需的任何值。试试这个:

代码语言:javascript
复制
let htmlTemplate = $("#description").text();
$(htmlTemplate).find('h4').each(function() {
  console.log($(this).html());
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/template" id="description">
  <div class="product-ddd">
    <h4>DETAILS &amp; DIMENSIONS</h4>
    <p>A trendy fashion-forward look has never been so comfortable. This full-length maxi dress for women is a must-have year-round style. Featuring a lovely scoop neckline, elbow-length sleeves, full A-line skirt, ankle-length hemline, and it is made from
      a soft comfortable stretch material. Pair this maxi dress with a denim or leather jacket for a stylishly edgy look. Available in four stylish colors and it is machine washable for easy care. Made in the USA from a comfortable stretch material.
    </p>
    <h4>FABRIC CONTENT &amp; CARE</h4>
  </div>
</script>

票数 3
EN

Stack Overflow用户

发布于 2021-10-12 13:57:46

您确定模板解析器仅替换{{ product.description }}吗?我怀疑它取代了整个<script></script>代码块。尝试这样做:

代码语言:javascript
复制
$(".product-ddd h4").each(function(){
    console.log($(this).html());
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69541792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档