最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - selecting all text within a div on a single left click with javascript - Stack Overflow

programmeradmin1浏览0评论

I have a simple non-clickable link within a div that looks like this:

It's meant to be a sharable link that the user can copy paste into other things.

For usability purposes, I want a single left click anywhere within the div to select the entire link:

I don't know much about, javascript/web programming, so I've tried the following:

<div id="share_link" onClick="select_all('share_link')"><%= request.url %></div>

and this javascript

<script type="text/javascript">
    function select_all(id) {
        document.getElementById(id).focus();
    }
</script>

This doesn't work. I'd like to know what's the simplest thing I should do to achieve what I want. I thought about changing the div to a text input or the text within to be a link, but ideally the content within should be read-only, non-editable, and non-clickable

I have a simple non-clickable link within a div that looks like this:

It's meant to be a sharable link that the user can copy paste into other things.

For usability purposes, I want a single left click anywhere within the div to select the entire link:

I don't know much about, javascript/web programming, so I've tried the following:

<div id="share_link" onClick="select_all('share_link')"><%= request.url %></div>

and this javascript

<script type="text/javascript">
    function select_all(id) {
        document.getElementById(id).focus();
    }
</script>

This doesn't work. I'd like to know what's the simplest thing I should do to achieve what I want. I thought about changing the div to a text input or the text within to be a link, but ideally the content within should be read-only, non-editable, and non-clickable

Share Improve this question asked Jan 2, 2011 at 12:40 tstyletstyle 7481 gold badge10 silver badges23 bronze badges 1
  • 1 Possible duplicate: stackoverflow.com/questions/985272/… – Damiqib Commented Jan 2, 2011 at 12:55
Add a comment  | 

2 Answers 2

Reset to default 18

This is achieved completely differently in IE compared to other browsers. The following will work in all major browsers:

<script type="text/javascript">
    function select_all(el) {
        if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
            var range = document.createRange();
            range.selectNodeContents(el);
            var sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
        } else if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
            var textRange = document.body.createTextRange();
            textRange.moveToElementText(el);
            textRange.select();
        }
    }
</script>

<div onclick="select_all(this)">Link text</div>

You can use jQuery for this with an input field:

$("#myInputField").focus(function(){
    // Select input field contents
    this.select();
});

You can mask the fact that it is an input field using the readonly attribute in the html:

    <input type="text" name="country" value="Norway"
  readonly="readonly" />

And use CSS to change the cursor so it won't hint a text input, something like:

cursor: crosshair;
发布评论

评论列表(0)

  1. 暂无评论