将页面上的选定文本插入到textarea(jQuery)中 [英] Insert selected text on the page into textarea (jQuery)

查看:76
本文介绍了将页面上的选定文本插入到textarea(jQuery)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要将页面上的选定文本插入到textarea中.必须有一些按钮可以做到这一点.

Need to insert selected text on the page into textarea. There must be some button to do it.

推荐答案

jQuery(function() {
    // Bind the click handler of some button on your page
    jQuery('#someButton').click(function(evt) {
        // Insert the selected text into a given textarea
        var textarea = jQuery('textarea#someTextArea');
        textarea.val(textarea.val() + getSelectedText());
        evt.preventDefault();
    });
});

// Get user selection text on page
function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection();
    }
    else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}

这篇关于将页面上的选定文本插入到textarea(jQuery)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆