English 中文(简体)
j Query - Wrap <span> around selected text - pornography bug
原标题:jQuery - Wrap <span> around selected text - duplicate instance bug

这个问题涉及一个类似的问题。 问题得到了回答,但这一回答有一个重大缺陷,即如果文件中所选择的同一案文在其他地方存在,则<代码><span>对复制件进行包装,而不是有关案文。

I realize this maybe be against some stackoverflow protocol, but I m posting here without a real clue as to how to proceed. My best guess would be to somehow find the string length before the selected text (something along these lines) but how to incorporate that in to the replacement function itself ... well I could use a push. Anyone?

(我已接过前一个职位的解决办法(如下:athias-bynens)。)

    $("p").live("mouseup",function() {
    selection = getSelectedText();
    if(selection.length >= 3) {
        var spn =  <span class="selected">  + selection +  </span> ;
        $(this).text($(this).html().replace(selection, spn));

    }
});

//Grab selected text
function getSelectedText(){
    if(window.getSelection){
        return window.getSelection().toString();
    }
    else if(document.getSelection){
        return document.getSelection();
    }
    else if(document.selection){
        return document.selection.createRange().text;
    }
}
最佳回答

I cheated, and used document.execCommand to wrap the selected text, then used the href (third parameter of the CreateLink execCommand) to find the element, wrap it with what I wanted, and then remove the link:

$("p").live("mouseup",function() {
    document.execCommand( CreateLink , false,  uniqueid );
    var sel = $( a[href="uniqueid"] );
    sel.wrap( <span class="selected" /> )
    sel.contents().unwrap();
});

文件.execCommand得到所有主要浏览器的支持,因此,你应该以这种方式安全地打捞。 在经过测试的浏览器中,浏览器本身将关闭和开放给你们的标签,这样,如果你从一个标题的中间点选择到另一个中间点,就应当正确打上标记。

问题回答

i think your key is to figure out what getSelection returns and work with that.

能够做到:

$(document.getSelection().anchorNode).wrap( <div style="color:blue"> )

选择文件.createRange()必须有类似的东西,让你发现所选择的东西。

例如:

$("p").live("mouseup",function() {
    var $sel = getSelectedElem();
    if($.trim($sel.text()).length >= 3) {
        $sel.warp( <span class="selected"> );
    }
});

//Grab selected text
function getSelectedElem(){
    if(window.getSelection){
        return $(window.getSelection().anchorNode);
    }
    else if(document.getSelection){
        return $(document.getSelection().anchorNode)
    }
    else if(document.selection){
        //todo figure out what to return here:
        return document.selection.createRange().text;
    }
    return $([]);
}

这一点是什么?

document.execCommand("insertHTML", false, "<span class= own-class >"+ document.getSelection()+"</span>");

source by Yepp The sMe





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签