Jquery:有没有办法突出显示一个Div的文字,当你点击它?

问题描述:

使用Jquery有一种方法来突出显示/选择(如果有人用鼠标选择它)我点击的div里面的文本?不是文本框,只是一个正常的div。

With Jquery is there a way to highlight/select(like if someone were to select it with the mouse) the text inside of a div that i click on? not a textbox, just a normal div.

我试图做一个短url框,当有人点击textarea时, ,但它也需要不允许人们改变文本框中的文本,但当一个文本框被禁用,你不能选择任何文本,所以我试图这样做,我只是认为一个div将是最简单的。

i'm trying to make a 'short url' box, where when someone clicks on the textarea, it highlights all the text, but it also needs to NOT allow people to change the text in the textbox, but when a textbox is disabled you can't select any text, so i'm trying to do that, i just thought a div would be easiest.

对不起,我没有做一个伟大的工作,解释我的意思,首先,添加信息上面澄清。

sorry guys i didn't do a great job of explaining what i meant at first, added info above to clarify.

右键,这不是关于背景颜色,而是关于选择文本。

Right, this isn't about background colours, it's about selecting the text.

只读,停止人们更改值:

First set an input to readonly, stopping people changing the value:

<input type="text" readonly="readonly" value="ABC" />

然后使用jQuery(或类似的)来选择文本一旦被点击:

Then using jQuery (or similar) to select the text once it's been clicked:

$('input').click(function() {
    $(this).select(); 
});

您应该按照自己的想法设置此输入样式,也许让它看起来像一个正常的文本位,查看此jsFiddle 进一步演示。

You should style this input as you see fit, perhaps to make it look like a normal bit of text, take a look at this jsFiddle for a further demonstration.