要求选中的文本块要不能有HTML代码字符,同时要满足常见文本的字符,如标点符号、英文标点、键盘上的上档符、中文的其他符号,即常见的查询句子,词语、书名等要可以提供搜索。
初稿脚本:
- function getSel()
- {
- var txt = '';
- var foundIn = '';
- if(window.getSelection)
- {
- txt = window.getSelection();
- foundIn = 'window.getSelection()';
- }else if(document.getSelection)
- {
- txt = document.getSelection();
- foundIn = 'document.getSelection()';
- }else if(document.selection)
- {
- txt = document.selection.createRange().htmlText;
- foundIn = 'document.selection.createRange()';
- }else
- return;
- document.getElementById("selectTxt").value = txt;
- }
- function specialCharacters(obj){
- var value = obj.value;
- // alert('验证前:'+value);
- var patrn=/^[0-9a-zA-Z\u4e00-\u9fa5+\.+\《》]+$/;
- if (patrn.test(value)) {
- return true;
- } else{
- alert("选中文字为非纯文本,其中含有特殊字符,请重选。");
- return false;
- }
- }
- function selectKM(){
- getSel();
- if(event.button==2){
- try{
- var content = document.getElementById("selectTxt").value;
- content = content.replace(/^\n+|\n+$/g,"");
- content = content.replace(/<\/?.+?>/g,"");
- content = content.replace(/[\r\n]/g, "");
- content = content.replace(/\s+/g,"");
- content = content.replace(""","");
- content = content.replace("\"","");
- content = content.replace("\"","");
- content = content.replace(" ","");
- content = content.replace("^","");
- content = content.replace("!=","");
- content = content.replace("{","");
- content = content.replace(";","");
- content = content.replace("?","");
- content = content.replace("'","");
- content = content.replace("'","");
- content = content.replace("\\","");
- content = content.replace("/","");
- content = content.replace("[]","");
- content = content.replace("*","");
- content = content.replace("(","");
- content = content.replace(")","");
- content = content.replace("{}","");
- content = content.replace(/%/g,"");
- content = content.replace("?","");
- content = content.replace("$","");
- content = content.replace("#","");
- content = content.replace("&","");
- content = content.replace("@","");
- content = content.replace("{","");
- content = content.replace("}","");
- content = content.replace("|","");
- content = content.replace("¥","");
- var contentPreVai = content;
- content = content.replace("——","");
- content = content.replace("—","");
- content = content.replace(".","");
- content = content.replace(",","");
- content = content.replace(/。/g,"");
- content = content.replace(/,/g,"");
- content = content.replace("!","");
- content = content.replace(";","");
- content = content.replace("‘","");
- content = content.replace("’","");
- content = content.replace(/、/g,"");
- content = content.replace("【","");
- content = content.replace("】","");
- content = content.replace(":","");
- content = content.replace("“","");
- content = content.replace("”","");
- content = content.replace("《","");
- content = content.replace("》","");
- content = content.replace("……","");
- document.getElementById("selectTxt").value = content;
- var vail = specialCharacters(document.getElementById("selectTxt"));
- if(contentPreVai.length>0 && vail == true){
- //if(contentPreVai.length>0 ){
- //alert(contentPreVai);
- createMenu(contentPreVai);
- }
- return;
- }catch(e){}
- }
- }
本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/1078224,如需转载请自行联系原作者