JS simple implementation element copy example

Source: Internet
Author: User

Source code:
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "Author" content = "">
<Title> content filtering </title>

<Script type = 'text/javascript '>

Function getPositions (){
Var el = document. getElementById ('shaixua ');
Var startPosition = 0; // start position of the selected text
Var endPosition = 0; // end position of the selected text
If (document. selection ){
// IE
Var range = document. selection. createRange (); // create a range object
Var drange = range. duplicate (); // clone an object

Drange. moveToElementText (el); // copy range
Drange. setEndPoint ('endtoend', range );

StartPosition = drange. text. length-range. text. length;
EndPosition = startPosition + range. text. length;
}
Else if (window. getSelection ){
// Firefox, Chrome, Safari etc
StartPosition = el. selectionStart;
EndPosition = el. selectionEnd;
}
Return {
"Start": startPosition,
"End": endPosition
}
}

// @ Todo get the selected text in textarea
Function getshaixuan (){
Var position = getPositions ();
Var start = position. start; // start position
Var end = position. end; // end position
Var text = document. getElementById ('shaixuany'). value;
Var selectText = text. substr (start, (end-start); // In textarea, the selected text
// Alert (selectText );
Var textBox1 = document. getElementById ("canjia ");
TextBox1.innerText = textBox1.value + selectText;
}
//]>
</Script>

<Title> copying an element </title>
<Style type = "text/css">
<! --
Body {font-size: 18px}
P {border-style: none}
. P1 {background-color: # FFFFFF; margin-top: 60px; margin-bottom: 3px; margin-left: 200px ;}
. P2 {background-color: # FFFFFF; margin-top: 2px; margin-bottom: 3px; margin-left: 150px ;}
. P3 {background-color: # FFFFFF; margin-top: 1px; margin-bottom: 3px; position: absolute; left: 550px; top: 60px}
. P4 {background-color: # FFFFFF; margin-top: 1px; margin-bottom: 3px; position: absolute; left: 500px; top: 85px}
. P5 {background-color: # FFFFFF; margin-top: 1px; margin-bottom: 3px; position: absolute; left: margin PX; top: margin PX}
-->
</Style>
</Head>

<Body>
<P class = "p1"> Filter list </p>
<Textarea id = "shaixuan" name = "check" cols = "23" rows = "15" class = "p2"> </textarea>
<Div class = "p3"> participants </div>
<Textarea id = "canjia" name = "canjia" cols = "23" rows = "15" class = "p4"> </textarea>
<Input type = "button" value = "content replication" class = "p5" onclick = "getshaixuan ();">
<! -- </Textarea> <button onclick = "getshaixuan ()"> get content </button> -->

</Body>
</Html>

Effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.