【问题标题】:Splitting an html tags拆分html标签
【发布时间】:2014-07-25 20:17:18
【问题描述】:
<div id="id1">
    "Hi HElP ME
    <br>
    <p>ok<p>
    <div>
         <img class = "some class src="">
    </div>
    <b>ok1<b>
    <div>
         <img class = "some class src="">
    </div>
    <p>end<p>
</div>

I want to split this html content such tha output will be:

$("#id1").find('img').each(function(){
    var result = somefunction(this)
    alert(result)
    //After first loop- "Hi HElP ME<br><p>ok<p>
    // Second time =<b>ok1<b>
    // third time  = <p>end<p>
})

谁能给我演示一下。由于没有显示图片数量,所以请给我一个通用的解决方案

【问题讨论】:

  • 您的&lt;div&gt; 中的id 不是id1
  • #id1 应该只是 #1,# 告诉 jQuery 它正在寻找一个 id。
  • 不要使用数字作为 id。
  • 这可能是有效的,但它不是一个好的做法——它根本没有语义价值。
  • 您的 HTML 格式不正确,无法返回您想要的结果。在某些divs 中有imgs,并且文本并不总是在同一位置(文本有时只在p 中)。您应该查看 $.text() 以返回 div 元素的内部文本。

标签: javascript jquery html split


【解决方案1】:
$('#id1').find('img').each(function(index){


var split_text = $(this).html().split(/<img[^>]*>/)[index];
alert(split_text)
});

【讨论】:

    【解决方案2】:

    您的 id 选择器似乎引用不正确。它应该是 $("#1") 而不是 $("#id1")。

    容易犯错,但很难找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2015-12-27
      • 2011-05-01
      相关资源
      最近更新 更多