Javascript don't add the class
Javascript don't add the class
大家好,javascript不要将类添加到html 中
$(".ocmessage").each(function(){
var text = $(this).find('p').html();
if(strpos(text,"<b>"+name+"</b>")!==false) $(this).addClass("quoteme");
});
该代码应该检测<p>...</p>
中是否有某个成员的名称,如果有javascript,则应该添加类quoteme
我该怎么修?
我想你是认真的。BTW、name
未定义。
var name = ''; // change the value
if(text.indexOf("<b>"+name+"</b>") > -1) {
$(this).addClass("quoteme");
}
假设ocmessage是div或其他包含类。
看看:http://jsfiddle.net/40vv7dbk/
$(".ocmessage").each(function () {
var $this = $(this);
var text = $this.find('p').html();
var name = "Ben"
// Will be -1 if not found.
if (text.indexOf(name) > -1) {
$this.addClass("quoteme");
}
});
它所做的是,当文档准备好时,使用类ocmessage遍历所有Div,查找
标记,然后检查其中是否有名称。如果是,我添加类的引号。
类为ocmessage
的元素可能包含多个段落。因此,在第一个each
-循环中,我们必须对所有<p>
进行第二个循环,如下所示:
$(".ocmessage").each(function(){
var $T = $(this);
$T.find('p').each(function() {
var text = $(this).html();
// find username and prevent multiple class adding
if(text.indexOf("<b>"+name+"</b>") > -1) {
$T.addClass("quoteme"); return false; // stop loop when class is added
}
});
});
在此处工作FIDDLE。归功于阿米特·佐基。
这是一种非常糟糕的完成任务的方法。以下是更标准的jquery方法。
$(".ocmessage").has('p b:contains('+name+')').addClass("quoteme");
相关文章:
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- Intercept the cellSubmit jqGrid
- CSS中的游标属性似乎不适用于USB On The Go
- 如何jQuery-Add按钮最多插入10个新输入
- 在不兼容的接收器上调用的方法Set.prototype.add未定义
- Add a class if var < 0 jquery
- jquery.html有效,但是.add不行;t
- 这个 add(x) 函数是如何工作的
- Sails.js/Waterline.add()和.remove()仅适用于第二次调用
- Appending the innerhtml of <th>
- Backbone - Collection.add() / Collection.create() 之间的区别
- jquery in the middle of html
- Can't get the tail to wag (animate) in three.js?
- jquery.proxy in the bootstrap-typeahead.js context
- What is the difference between `HTMLImports.whenReady` and `
- 在什么情况下,Array.observe的“add”事件会触发
- Javascript add class 不起作用
- 从sql填充Jqgrid的自定义Add函数中的下拉数据
- JQuery DataPicker add on the
- Javascript don't add the class