Salut J'ai quelques problèmes:comment obtenir texte sélectionné entourant le contexte dans javascript dans le paragraphe différent?
- Qu'est-ce que je pourrais passer le mot autour du mot sélectionné, si le mot avant et après le mot sélectionné limites indiquées seulement 20 mots qui entourent le mot sélectionné?
- comment obtenir la position correcte si le mot dans un paragraphe ont le même mot, par exemple, j'ai une phrase comme ceci: foo laa foo doo puis-je choisir le mot « foo » dont la position est entre les mots laa et doo?
comment obtenir un mot à partir d'un autre paragraphe?
par exemple:
p1: J'aime la chemise rouge
p2: ma mère n'a pas aimé la chemise rouge
le mot que je choisi est « mère », et je pour prendre 10 mots autour du mot "mère" qui est "J'aime la robe rouge" et "Je n'aime pas la chemise rouge."
notes: question n ° 2 est en mesure d'utiliser le nextSibling et previousSibling?
ceci est mon code que j'essaie:
<html>
<head>
<script type="text/javascript">
function getElements(){
var x = document.getElementsByTagName("body");
x = x[0].innerHTML;
x = x.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";});
var str = x.replace(/<\/?[^>]+(>|$)/g, "");
var emailPattern = /[_a-zA-Z0-9\.][email protected][\.a-zA-Z0-9]+\.[a-zA-Z]+/gi;
var urlPattern = /[a-z]+:\/\/[^\s]+/gi;
var numberOrSymbolPattern = /[0-9\.,[email protected]#\$%\^\&*\(\)`~_\-=\+|\\{}\[\]\s:;<>\?\/]+/gi;
//////alert(str);
var str = str.replace(emailPattern , " ");
var str = str.replace(urlPattern , " ");
var str = str.replace(numberOrSymbolPattern , " ");
//////alert(str);
var str = str.replace(/[\n\f\r\t]/g, " ");
var hilangtandabaca = str.replace(/[.!:;'",?]/g," ");
var kataptg = hilangtandabaca;
//alert(kataptg);
var kata = new Array();
kata[0] = " is ";
kata[1] = " the ";
kata[3] = " of ";
kata[4] = " a ";
kata[5] = " or ";
kata[6] = " for ";
kata[7] = " from ";
kata[8] = " in ";
kata[9] = " this ";
kata[10] = " and ";
kata[11] = " on ";
kata[12] = " with ";
kata[13] = " my ";
for(var i=0,regex; i<kata.length; i++){
var regex = new RegExp(kata[i],"gi");
kataptg = kataptg.replace(regex," ");
}
var select = getSelected()+ "";
alert(select);
var index = kataptg.indexOf(select);
//alert("indeks select text:" + index);
if (index >= 0) {
var strBefore = "";
var strAfter = "";
//var strOri ="";
//if (index = -1)
//strOri = kataptg.substr(index);
//alert(strOri);
if (index > 0)
strBefore = kataptg.substr(0, index);
//alert(strBefore);
if (index < kataptg.length - 1)
strAfter = kataptg.substr(index + select.length, kataptg.length - (index + select.length));
//alert(strAfter);
alert("Before: " + strBefore + "\nAfter: " + strAfter);
}
}
function getSelected() {
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection();
} else if (document.selection) {
userSelection = document.selection.createRange();
}
return userSelection;
}
</script>
</head>
<body>
<h2>About</h2>
<p> my email : [email protected]
<p> my url http://id.wikipedia.org/wiki/URL
<p> my telepon number = 081330782
<p>okey In agriculture, the harvest is the process of gathering mature crops from the fields. Reaping is the cutting of grain or pulse for harvest, typically using a scythe, sickle, or reaper.[1] The harvest marks the end of the growing season, or the growing cycle for a particular crop, and this is the focus of seasonal celebrations of on many religions. On smaller farms with minimal mechanization, harvesting is the most labor-intensive activity of the growing season great yeah. !:;'",?</p>
<p>
<input type="button" onclick="getElements()" value="ambil select text" />
</p>
</body>
</html>
Je vous ai donné une réponse raisonnable à une question très semblable à cela avant (http: //stackoverflow.com/questions/4332753/how-to-get-selected-text-but-can-i-get-surrounding-context-in-j avascript/4333439 # 4333439), que vous semblez ignorer. Pourquoi l'avez-vous ignoré? Si vous ne l'avez pas compris, vous pouvez demander une clarification dans un commentaire. –