I've changed my code, so now lets say there is this:
<p>
<a href="#">@Mr. Jingles</a>
<br>
<br>
This would be where the person would say something. // I want to start here
<br>
<br>
This is even more text.
</p>
<p>
<a href="#">@Lady Ladington</a>
<br>
<br>
Hello!
</p>
What I want to do is delete the first <a>
element, then dele开发者_Go百科te all of the <br>
elements until where the person would say something.
When successful, it should look like:
<p>
This would be where the person would say something.
<br>
<br>
This is even more text.
</p>
<p>
Hello!
</p>
Note: These would be people commenting on a blog post.
I can only think of using Regexp in the innerHtml property of the <p></p>
.
function remove (elem, name) {
var content = elem.innerHTML,
regex = new RegExp('@<a.*?>' + name + '<\/a>(\n)*(<br\s?\/?>|\n)*', 'mgi');
elem.innerHTML = content.replace(regex, '');
}
remove(paragr, 'Wiki Tiki');
And you should probably work on the regular expression, to handle all your scenarios, you could get newline characters \n
and/or xhtml breaks <br />
Problem is that it's going to be a bit slow because of the innerHTML manipulation, but try it out, maybe it'll be fast enough.
Assuming it's wrapped in an element with the ID of "theContent":
var content = document.getElementById('theContent');
var c = content.innerHTML;
c = c.replace(/@<a([^\/])+\/a>/,'');
c = c.replace(/(\<br>(\s)+)+/,'');
content.innerHTML = c;
This probably has to take into account some more checks, but it could be something like this:
$('p')
.children().filter(function() {
return this.nodeType == Node.TEXT_NODE && $(this).text() == '@';
})
.next('a')
.nextUntil(':not(br)')
.andSelf()
.andSelf()
.remove();
Also, this will not work in IE7 and earlier. For that use this.nodeType == 3
.
You could do something like this, I'm not sure how efficient it would be, and you may have to alter the logic depending on the text contained in your page -
$("p").contents().each(function() {
if (this.nodeType == 3 && this.nodeValue.indexOf("This would be") > -1) return false;
$(this).remove()
})
The code just removes all nodes until it finds the text node with the words "This would be" in it.
Demo - http://jsfiddle.net/ipr101/TTuDw/3/
精彩评论