For example, I'm using the following function:
$('body').html( function(i,html) {
var arr = html.split(". ");
arr[arr.length-1]="<span class='findme' >"+arr[arr.length-1]+"</span> ";
alert(arr)
return arr.join(". "开发者_Python百科);
});
The problem is it moves the cursor away from where the user was typing. Has anyone else seen this?
By setting the HTML of any element you're essentially resetting all of its content! All event handlers, all states and DOM properties, and this includes any element that currently has 'focus'.
Use the DOM API as it was intended.
It looks like you want to wrap the content following the last period in the page with a <span>
. To do this properly you'd need to traverse through the DOM checking for text-nodes that contain the period and then replace part of the last-found text node with a <span>
element.
精彩评论