使用jQuery从段落中选择行 [英] Select line from paragraph using jQuery

查看:94
本文介绍了使用jQuery从段落中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的段落:

some text1. some text2. some text3. some text4. some text5. some text6. some text7.

我想使用jQuery在鼠标悬停时从上面的段落中仅选择'some text2',当我单击它时,所选文本应存储在变量中.

i want to select only 'some text2' from above paragraph onmouseover using jQuery and when i click on it the selected text should store in variable.

该怎么做?

推荐答案

您的问题有点令人困惑,但据我了解,您希望句点(和空格)之间的任何内容都可以选择元素?怎么样:

Your question is a little confusing but from what I understand, you'd like whatever is between the full stops (and the space) to be a select-able element? How is this:

splitter = ". ";
$(function(){
    words = $("#text").text().split(". ");
    $("#text").html("");
    for(i=0; i< (words.length-1); i++){
        $("#text").append("<span class='textPart'>"+words[i]+"</span>"+ splitter );
    }

    $("#text").delegate(".textPart", "mouseover", function(){
        $("#hovered").html($(this).text()); 
    });
});

这是一个演示

这篇关于使用jQuery从段落中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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