简体   繁体   English

使用javascript / jquery选择文本

[英]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. 我有一个服务器查询,查询一个我的世界服务器并显示其状态,它将ip和服务器名称存储在一个变量中,并在单个循环中用jquery显示它。

I want the dispalyed server name to be selectable onclick, but whatever i try it doesnt work 我希望显示的服务器名称可以在onclick上选择,但是我尝试执行的任何操作均不起作用

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. 这里的servername是包含服务器名的变量,server ip包含ip,servercontainer是显示所有数据的div的类。

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. 当一个人单击文本时,必须突出显示整个文本,以便他要做的就是ctrl + c复制它。

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... 没有更多上下文很难回答,但是假设您只是试图获取被单击项的文本值,请尝试使用.text()方法...

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. ...,而不是.html()如果您只想要文本)。

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/ 现场演示, 网址http://jsfiddle.net/Wx4YN/1/

If yes, don't forget that this need jQuery 如果是的话,别忘了这需要jQuery

Have some little bugs when you use div or p. 使用div或p时会有一些小错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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