简体   繁体   中英

Selecting text with javascript/jquery

I have a server query, that queries a minecraft server and displays its status.It stores the ip and the server name in a variable and displays it with jquery in a single loop.

I want the dispalyed server name to be selectable onclick, but whatever i try it doesnt work

the code

$('.servercontainer .serverdata span[rel=\"'+serverarray[i]+'\"]').html(servername[i]);

here servername is the variable which contains the servername, server ip contains the ip and servercontainer is the class of the div that displays all the data.

I want to be able to select the server name when clicked on.

Thanks for reading this
edit: the servername is what i want selected. When a person clicks on the text, the whole text has to be highlighted so that all he has to do is ctrl+c to copy it.

Sorry for the confusion

不确定要理解..如果您要让服务器名可单击并链接到服务器IP,则可以执行以下操作:

$('.servercontainer .serverdata span[rel=\"'+serverarray[i]+'\"]').html("<a href='"+serverip[i]+"'>"+servername[i]+"</a>");

Hard to answer without more context, but assuming you're just trying to grab the text value of the clicked item, try using the .text() method...

example:

$('.servercontainer .serverdata span[rel=\"'+serverarray[i]+'\"]').click( function () {
  var capturedText = $(this).text();
  // do something with the captured text...
});

... instead of .html() if you just want the text.

Do you mean this:

<SCRIPT>$(document).ready(
    function() {
        $('.copyText').click(
            function() {
                if ($('#tmp').length) {
                    $('#tmp').remove();
                }
                var clickText = $(this).text();
                $('<textarea id="tmp" />')
                    .appendTo($(this))
                    .val(clickText)
                    .focus()
                    .select();
        return false;
    });
$(':not(.copyText)').click(
    function(){
        $('#tmp').remove();
    });

});
</SCRIPT>
<SPAN CLASS="copyText">Click Me!</SPAN>
<SPAN>Not Me!</SPAN>

Live Demo at http://jsfiddle.net/Wx4YN/1/

If yes, don't forget that this need jQuery

Have some little bugs when you use div or p.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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