css - 使用悬停在 div 上更改该 div 内的所有元素

标签 css html hover

我提前道歉,因为这似乎是一个关于对 CSS 和 Javascript 的非常基本的理解的问题。

我想做的是:想象一个包含 h3 和 p 的 div。在 div 上悬停时,我希望 h3 和 p 更改它们的字体粗细。到目前为止,我在这里使用这段代码来更改悬停在 div 上的不透明度和边框,但我真的不知道如何引用 div 中的两个元素。真的很抱歉,但我需要有人用非常简单的术语向我解释。

例如,我认为 div 中的那些元素称为子元素,但我什至不确定......我真的是第一次使用所有 HTML/CSS/Java 东西并尝试随着我的进行,弄清楚事情。到目前为止,我找到的教程网站无法解决我的问题,因此发布了这篇文章。

更多背景信息:我正在使用 "smoothgallery" script由 jondesign (Jonathan Schemoul) () 设计并试图按照我的意愿改变它,但如果您不知道它的实际工作原理,那将非常困难。我在其中执行脚本的站点可以找到 here .

这是在悬停时更改 div 的 CSS 部分:

.jdGallery .gallerySelector .gallerySelectorInner div.hover{
    border: 1px solid #89203B;
    border-left: 0.8em solid #89203B;
    background: url('../../images/teaserBox_bg.jpg') no-repeat;
    background-size: 100% 100%;
    filter:alpha(opacity=1);
    -moz-opacity:1;      /
    -khtml-opacity: 1;
    opacity: 1;
}

CSS 文件中的这个条目更改了设置,例如那个 div 里面的 h3,

.jdGallery .gallerySelector .gallerySelectorInner div.galleryButton h3{
    margin: 0;
    padding: 0;
    font-size: 12px;
    font-weight: normal;
}  

您可能还想看一下生成这些类的 .js 文件,它可以在 here 中找到。 .

这可能是这里最重要的部分:

    createGalleryButtons: function () {
            var galleryButtonWidth =
                    ((this.galleryElement.offsetWidth - 30) / 2) - 14;
            this.gallerySet.each(function(galleryItem, index){
                    var button = new Element('div').addClass('galleryButton').injectInside(
                            this.gallerySelectorInner
                    ).addEvents({
                            'mouseover': function(myself){
                                    myself.button.addClass('hover');
                            }.pass(galleryItem, this),
                            'mouseout': function(myself){
                                    myself.button.removeClass('hover');
                            }.pass(galleryItem, this),
                            'click': function(myself, number){
                                    this.changeGallery.pass(number,this)();
                            }.pass([galleryItem, index], this)
                    }).setStyle('width', galleryButtonWidth);
                    galleryItem.button = button;
                    var thumbnail = "";
                    if (this.options.showCarousel)
                            thumbnail = galleryItem.elements[0].thumbnail;
                    else
                            thumbnail = galleryItem.elements[0].image;
                    new Element('div').addClass('preview').setStyle(
                            'backgroundImage',
                            "url('" + thumbnail + "')"
                    ).injectInside(button);
                    new Element('h3').set('html', galleryItem.title).injectInside(button);
                    new Element('p').addClass('info').set('html', formatString(this.options.textGalleryInfo, galleryItem.elements.length)).injectInside(button);
            }, this);
            new Element('br').injectInside(this.gallerySelectorInner).setStyle('clear','both');
    },

所以我的问题是,是否可以通过使用主 div 上的悬停功能来更改 h3 和 p 设置?

提前致谢!同样对于负面批评,我真的不知道我发布这个问题的方式是否做错了,我什至可以在这里提问。

最佳答案

你让这种方式变得比它需要的更复杂。不需要 Javascript 来执行此操作。假设您有以下内容:

<div class="container">
    <h3>This is a header</h3>
    <p>This is a paragraph</p>
</div>

所以你有一个容器,带有标题和段落。比方说你想要标题正常重量,红色段落正常,整个东西周围有一个填充框。这是您的样式:

.container { border: 1px solid black; padding: 10px; }
.container h3 { font-weight: normal; }
.container p { color: red; }

当您将鼠标悬停在 上时,您希望段落和标题为粗体并且框边框变为蓝色。将其添加到您的样式表(或 <style> block )下方上方的 CSS:

.container:hover { border-color: blue; }
.container:hover h3 { font-weight: bold; }
.container:hover p { font-weight: bold; }

请注意,您可以节省一点空间,并通过组合 <h3> 使其更简洁。和 <p>样式用逗号排成一行,因为它们是一样的。整个事情现在看起来像这样:

.container { border: 1px solid black; padding: 10px; }
.container h3 { font-weight: normal; }
.container p { color: red; }
.container:hover { border-color: blue; }
.container:hover h3, .container:hover p { font-weight: bold; }

请记住,“CSS”中的“C”代表“级联”:样式在两个层次结构中向下级联(也就是说,父元素的样式也适用于子元素,除非它具有默认样式,如边距或其他) , 以及样式表的下方 - 这意味着如果您在其他人之后定义的样式应用于同一元素,则它们将覆盖它们。

CSS 中的“:hover”选择器几乎可以用在任何东西上,只有极少数异常(exception)。我经常将它们用于无 Javascript 的下拉菜单。您可以在此处找到有关“:hover”CSS 选择器的更多信息:W3Schools CSS reference on ":hover" .事实上,W3Schools site通常是温习 CSS 的好资源。

关于css - 使用悬停在 div 上更改该 div 内的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531366/

相关文章:

html - 内联 block div 未水平对齐

HTML 宽度 100%

html - 如何为匹配一个或另一个的元素组合属性选择器?

html - 如何从表格的悬停中排除悬停的 td?

javascript - "textLength"Internet Explorer 中 svg 文本元素悬停时发生变化

javascript - 单击图像时 Div 不执行 slideToggle()

html - 为什么我的导航栏扩展不正确?

javascript - 单击时 Angular 功能将不起作用

html - 如何让 translateX 转换在 firefox 中工作?

jQuery 屏蔽输入 : How do I animate the appearance of the text?