0

i have a paragraph like:

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

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.

how to do this?

4

3 回答 3

1

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()); 
    });
});

Here's a demo

于 2013-06-11T10:38:09.990 回答
1
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()); 
});
});
于 2013-07-22T06:18:06.520 回答
-1

Try using :contains

<p>some text1. some text2. some text3. some text4. some text5. some text6. some text7.</p>

jQuery:

$(document).ready(function(){
  $('p').hover(function(){
  var s = $('p').text();
  alert(s.indexOf("some text2") !== -1);
  //true if it contains
  //false if it not ontains
   });
});
于 2013-06-11T10:29:00.643 回答