Jquery 在另一个div中更改div
我的html看起来像:Jquery 在另一个div中更改div,jquery,Jquery,我的html看起来像: <div class="cal_wrapper_top" id="event-1"> <div class="cal_date"> Feb 18 </div> <div class="cal_events"> Jack Johnson </div> </div> <div class="cal_wrapper" id="event
<div class="cal_wrapper_top" id="event-1">
<div class="cal_date">
Feb 18
</div>
<div class="cal_events">
Jack Johnson
</div>
</div>
<div class="cal_wrapper" id="event-2">
<div class="cal_date">
Feb 19
</div>
<div class="cal_events">
Jason Mraz
</div>
</div>
2月18日
杰克·约翰逊
2月19日
杰森·姆拉兹
当您将鼠标悬停在event-1或event-2上时,我希望将id=date的div加粗
是否可以选择此特定div?您可以使用纯CSS执行此操作: 或者您可以使用jQuery来实现(尽管我不知道您为什么想要): 此外,您还可以一次性使用ID
编辑:下面是一个更改悬停文本的示例:您可以使用纯CSS执行此操作: 或者您可以使用jQuery来实现(尽管我不知道您为什么想要): 此外,您还可以一次性使用ID 编辑:下面是一个更改悬停文本的示例:当然:
$('div.cal_wrapper, div.cal_wrapper_top').hover(function() {
$(this).find('div#date').css('font-weight', 'bold');
//$(this).find('div.date').css('font-weight', 'bold'); //if changing to class
//$(this).find('div[name="date"]').css('font-weight', 'bold'); //if changing to name
}, function() {
$(this).find('div#date').css('font-weight', 'normal');
//$(this).find('div.date').css('font-weight', 'normal'); //if changing to class
//$(this).find('div[name="date"]').css('font-weight', 'normal'); //if changing to name
});
我要指出的是,id
属性对于页面上的单个元素来说应该是唯一的。否则,怪癖肯定会发生
您可能需要重做html并将id
更改为类:
<div class="cal_wrapper_top" id="event-1">
<div class="cal_date date">
Feb 18
</div>
<div class="cal_events">
Jack Johnson
</div>
</div>
<div class="cal_wrapper" id="event-2">
<div class="cal_date date">
Feb 19
</div>
<div class="cal_events">
Jason Mraz
</div>
</div>
当然可以:
$('div.cal_wrapper, div.cal_wrapper_top').hover(function() {
$(this).find('div#date').css('font-weight', 'bold');
//$(this).find('div.date').css('font-weight', 'bold'); //if changing to class
//$(this).find('div[name="date"]').css('font-weight', 'bold'); //if changing to name
}, function() {
$(this).find('div#date').css('font-weight', 'normal');
//$(this).find('div.date').css('font-weight', 'normal'); //if changing to class
//$(this).find('div[name="date"]').css('font-weight', 'normal'); //if changing to name
});
我要指出的是,id
属性对于页面上的单个元素来说应该是唯一的。否则,怪癖肯定会发生
您可能需要重做html并将id
更改为类:
<div class="cal_wrapper_top" id="event-1">
<div class="cal_date date">
Feb 18
</div>
<div class="cal_events">
Jack Johnson
</div>
</div>
<div class="cal_wrapper" id="event-2">
<div class="cal_date date">
Feb 19
</div>
<div class="cal_events">
Jason Mraz
</div>
</div>
ID应该是唯一的。不能在同一文档中多次重复使用ID。请改用类名。请永远忘记这一点。即使如此,是否有可能只从该ID更新类cal_date?ID应该是唯一的。不能在同一文档中多次重复使用ID。请改用类名。请永远忘记这一点。即便如此,是否有可能仅从此更新类cal_date?我本来打算建议一个JavaScript解决方案,但后来您发布了这个。干得好,你有我的+1。很好的解决方案!如果我想把div的内容改成日期以外的内容,你有什么想法吗?@Mike你必须使用Javascript。我真的不在乎。。。我不再为IE6设计了,在我看来这是一种浪费。我认为在这一点上,如果你需要IE6的支持,你应该提前声明。人们正忙着从大脑中清除这些信息。我本想提出一个JavaScript解决方案,但后来你发布了这个。干得好,你有我的+1。很好的解决方案!如果我想把div的内容改成日期以外的内容,你有什么想法吗?@Mike你必须使用Javascript。我真的不在乎。。。我不再为IE6设计了,在我看来这是一种浪费。我认为在这一点上,如果你需要IE6的支持,你应该提前声明。人们正忙着从脑子里清除这些。据我所知,
name
属性只在表单中使用。name
属性可以在任何元素上使用(尽管它不一定只在任何元素上有效)title
或alt
或自定义属性也可以轻松使用。如果这是我的项目,我会使用class
。我一直在努力说服所有愿意听我说的人,在现代浏览器上,类选择器只比IDs慢一点,而且它们是正确的。随着需求的变化,ID停止工作,但类基本上永远是好的。故事的寓意:在做一些奇怪的事情之前先进行基准测试。据我所知,name
属性只在表单中使用。name
属性可以在任何元素上使用(尽管它不一定在任何元素上都有效)title
或alt
或自定义属性也可以轻松使用。如果这是我的项目,我会使用class
。我一直在努力说服所有愿意听我说的人,在现代浏览器上,类选择器只比IDs慢一点,而且它们是正确的。随着需求的变化,ID停止工作,但类基本上永远是好的。这个故事的寓意是:在做一些奇怪的事情之前先进行基准测试。