JavaScript implementation of the word tag + word search function

Source: Internet
Author: User
Tags tagname
Javascript

Use JavaScript method to select the text of the mouse with emphasis on style, but also the search function. Demo effect, please see this blog page with the mouse text selection effect

The complete sample code is as follows

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>


<script language=javascript>
<!--

Document.body.onload=adddiv;
Document.onmousedown=recordobj;
Document.ondblclick=dbclick;
Document.onmouseup=showselect;
var starobj,isdb=false,allow=true;

function Isallow ()
{
if (allow) {
Allow=false;
Alert (' is closed ');
}
else{
Allow=true;
Alert (' is opend ');
}

}

function Dbclick ()
{
Isdb=true;
}
function Recordobj ()
{
Starobj=event.srcelement;
}
function Showselect () {
var str= "";
if (event.srcelement.tagname!= "A" &&event.srcelement.tagname!= "INPUT" &&event.srcelement==starobj &&!isdb&&allow)
{
var otext=document.selection.createrange ();

if (otext.text.length>0)
{
Str=otext.text;
otext.text= "BuB" +otext.text+ "Eue";
}


Otext.select ();
Event.srcelement.innerhtml=event.srcelement.innerhtml.replace ("BuB", "<u style=" font-weight:bold; COLOR: #ff3366 ' > '). Replace ("Eue", "</u>");

}

Searchgoogle (str)
Isdb=false;
}

function Searchgoogle (str)
{
var Obj=document.getelementbyid ("Searchgoogle");
if (str.length>0)
{
obj.style.display= "Block";
obj.style.position= "Absolute";
obj.style.zindex=999;
obj.style.postop=document.body.scrolltop+event.y-25;
obj.style.posleft=document.body.scrollleft+event.x+5;
obj.style.widht=80;
Obj.innerhtml= "<a target=_blank href=http://www.google.com/search?ie=utf-8&oe=utf-8&q=" +str+ "style=" Border-right:royalblue thin solid; Border-top:royalblue thin solid; Font-weight:bold; Border-left:royalblue thin solid; Clip:rect (auto auto, Auto Auto); COLOR: #ffffff; Border-bottom:royalblue thin solid; Background-color:inactivecaption; Text-decoration:none ' >search it! </a> ";

}
Else
{
Obj.style.display= "None";
}
}

function Adddiv ()
{
var mobj = document.createelement ("div");

Mobj.id= "Searchgoogle";

Document.body.appendChild (Mobj);
}

-->
</SCRIPT>
<input type= "button" value= "Off/Open stroke function" >
<p>qrasfdasfasfdasfsafasdfsafsafasdfasdfasd</p>
</body>

Adding this string of code to the page also has a search function. Demo effect, please see this blog page with the mouse text selection effect

Before I could tidy up the code, it looked a bit messy.



Related Article

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.