每个< p>的第一个单词选择器在< div>中 [英] First word selector for every <p> within a <div>

查看:90
本文介绍了每个< p>的第一个单词选择器在< div>中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已参考以下问答:第一个单词选择器

我正在寻找使用相同类型的<span></span>设置,但是,我希望它出现在特定<div>中每个<p>的每个第一个单词中.我该如何指定呢?

I am looking to use the same sort of <span></span> setup, however, I would like for it to occur for every first word of every <p> within a particular <div>. How can I specify this?

    $(window).load(function(){
(function () { 
    var node = $("p").contents().filter(function () { return this.nodeType == 3 }).first(),
        text = node.text(),
        first = text.slice(0,text.indexOf(" "));

    if (!node.length)
        return;

    node[0].nodeValue = text.slice(first.length);
    node.before('<span>' + first + '</span>');
})();
  });

然后在样式表中,我可以采用应用于各种选择器的简单<span></span>,并指定"span"函数将对每个选择器执行的操作.

Within the stylesheet, I then can take the simple <span></span> that's been applied to various selectors, and specify what the 'span' function will do with each selector.

推荐答案

编辑

dakdad有一个更好的解决方案:小提琴

@Michael Cornett:您能将dakdad标记为答案吗? :)

@Michael Cornett: could you mark dakdad as answer please? :)

这篇关于每个&lt; p&gt;的第一个单词选择器在&lt; div&gt;中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆