本文介绍了javascript更改标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JavaScript突出显示标记中的文本.我可以成功更改标记,如浏览器开发人员工具(chrome)所示,但是在浏览器窗口中显示的渲染代码不会更改.有人看到我在做什么错吗?
I am using javascript to highlight text in markup. I can successfully change the markup, as seen in the browser developer tool (chrome), but rendered code as appears in browser window does not change. Does anyone see what i am doing wrong?
<script type="text/javascript">
$(document).ready(function (e) {
$('#submit').click(function (e) {
try {
var $inputTxt = $('#search');
result = $inputTxt.val();
alert(result);
$('p').each(function () {
var a=$(this).html().toString();
var aindx=[];
while(a.indexOf(result) >= 0){
//alert("string found "+a.indexOf(result)+" position");
var b=a.substring((a.indexOf(result)+result.length));
if(aindx.length > 0) {
var prev=aindx.length-1;
aindx[aindx.length]=a.indexOf(result)+aindx[prev];
$(this).html(a.substring(0,(aindx[aindx.length]-1))+'<mark id="tmprry">'+result+'</mark>'+a.substring(aindx[aindx.length+result.length-1]));
} else {
aindx[0] =a.indexOf(result);
$(this).html(a.substring(0,(aindx[0]-1))+'<mark id="tmprry">'+result+'</mark>'+a.substring(aindx[0]));
}
//insert mark around position
a=b;
}
});
} catch (ex) { e.preventDefault(); }
});
});
</script>
推荐答案
这篇关于javascript更改标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!