简体   繁体   English

在另一个 html 元素之前将纯文本包装在 div 中

[英]wrap plain text inside a div before another html element

Ok, that probably doesn't make a ton of sense... let me illustrate:好吧,这可能没有多大意义......让我来说明一下:

<div class="csl-entry"> lorem ipsum yada yada yada... <a title="some title" rel="external" href="http://google.com">Google</a> </div>

I would like to wrap the "lorem ipsum yada yada yada..." in a <p> tag, but am unsure how to proceed without also wrapping the <a> .我想将“lorem ipsum yada yada yada ...”包装在<p>标签中,但我不确定如何在不包装<a>的情况下继续进行。

If it's easier to do in JavaScript or jQuery, I'm fine with that, too.如果在 JavaScript 或 jQuery 中更容易做到,我也可以。 Much of my site is semi-dependent on a bit of JS anyway.无论如何,我的大部分网站都半依赖于一点 JS。

Thanks!谢谢!

Wrapping a <a> tag inside a <p> tag inside a <div> is perfectly ok.<a>标签包裹在<div>内的<p>标签内是完全可以的。

So I'd say just do:所以我会说只是做:

<div class="csl-entry"> 
     <p>
          lorem ipsum yada yada yada... 
          <a title="some title" rel="external" href="http://google.com">Google</a>
     </p>
</div>

If I understand your question well that is... :)如果我很好地理解你的问题,那就是...... :)

Using jquery使用 jquery

$('.csl-entry').contents().filter(function() {

  return this.nodeType == 3;

}).wrap('<p></p>')
            })

Please note that this will wrap all text nodes that are children of csl-entry - so if you have any stray spaces in the div you will get an empty paragraph tag.请注意,这将包装作为 csl-entry 子级的所有文本节点 - 因此,如果您在 div 中有任何杂散空间,您将获得一个空的段落标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM