jQuery pull out text inside div but not in p tag

旧城冷巷雨未停 提交于 2019-12-23 17:25:18

问题


In the below code I would like to put a variable to the text within the div but NOT whats inside the p tag:

 <div class="review-content merchant-review-content">
     I want to grab this text
     <p class="review-rating">
     From a cool person
     - My Date goes here
     -
     A Review
    </p>
    <div></div>
</div>

Additionally I would like to only pull the first 250 characters of it, if thats possible!


回答1:


I found this tutorial: http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html

Final jsFiddle: http://jsfiddle.net/w9N8N/3/




回答2:


Try this snippet:

$("div.review-content").clone().children('p').remove().end().html().substring(0,250);


来源:https://stackoverflow.com/questions/9535674/jquery-pull-out-text-inside-div-but-not-in-p-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!