简体   繁体   English

无法获得正确的CSS选择器

[英]Trouble getting the right CSS selector

I'm having trouble getting the right selector. 我在选择合适的选择器时遇到了麻烦。

Let me try to explain the best way I can: 让我尝试解释我可以做到的最好方法:

I'm working on a project that I cannot change HTML and Javascript, it has some dynamic HTML and other reasons. 我正在做一个无法更改HTML和Javascript的项目,它有一些动态HTML和其他原因。

On the project, there is an image on a <img> tag. 在项目上, <img>标记上有一个图像。

However, I need to change colors between two layouts, and as you can see on the HTML/CSS the only way I got that to work is to hide th <img> tag and set a background to the anchor, that has a title. 但是,我需要在两种布局之间更改颜色,正如您在HTML / CSS上所看到的那样,使我工作的唯一方法是隐藏<img>标记,并为具有标题的锚点设置背景。

So, now, when I change the layouts, the image changes, however there is also something else, this image on click hides the menu and changes the image one more time. 因此,现在,当我更改布局时,图像也会更改,但是还有其他事情,单击此图像会隐藏菜单并再次更改图像。

Now, I need to hide the background on the anchor when the title on the image changes. 现在,当图像标题更改时,我需要隐藏锚点的背景。

Here is the HTML BEFORE clicking the image 这是单击图像之前的HTML

<div id="div-mh-ico">

<ul id="ul-icone-mh" class="icones">
    <li>
        <a href="#" class="mh-icon" title="Esconder menu horizontal" onclick="hideMenuHorizontal();">
            <img title="Esconder menu horizontal" id="imgHideMenu" src="images/ico_hidemh.png" width="16" height="16">
        </a>
    </li>
</ul>

And here is the HTML AFTER I click on the image 这是我点击图片后的HTML

<div id="div-mh-ico">

<ul id="ul-icone-mh" class="icones">
    <li>
        <a href="#" class="mh-icon" title="Esconder menu horizontal" onclick="hideMenuHorizontal();">
            <img title="Exibir menu horizontal" id="imgHideMenu" src="images/ico_showmh.png" width="16" height="16">
        </a>
    </li>
</ul>

THE CSS CSS

I HIDE THE ORIGINAL IMAGE, USED ON THE OTHER LAYOUT 我隐藏了在其他布局上使用的原始图像

#ul-icone-mh li a img {
    visibility: hidden !important;
}

AND SET THE NEW IMAGE 并设置新图像

a[title="Esconder menu horizontal"] {
box-sizing: border-box;
background-image: url(../images/ico_hidemhc.png);
background-size: 16px;
background-repeat: no-repeat;
}

And when I click it, the image stays the same, but I need to hide that image when the title changes and add another image. 当我单击它时,图像保持不变,但是当标题更改时,我需要隐藏该图像并添加另一个图像。

Any ideas what I can do? 有什么想法我能做什么?

You need a bit more than just the right CSS selector. 您不仅仅需要正确的CSS选择器。 The problem there is the old stumbling block that there is no parent selector. 问题是没有父选择器的旧绊脚石。

A bit more thought and work is required. 需要更多的思考和工作。

 img { height:50px; width:50px } ul {padding: 0; list-style:none;} .icones a::after { content: ''; height:50px; width:100px; background-image: linear-gradient(to right, #00FF00 50%, #0000FF 50%); display:inline-block; } .icones a { height:50px; width:50px; display:block; white-space:nowrap; overflow:hidden; font-size:0; } .icones a img[title='Esconder menu horizontal'] { margin-left: -50px; } .icones a img[title='Exibir menu horizontal'] { margin-left: -100px; } 
  <div id="div-mh-ico"> <ul id="ul-icone-mh" class="icones"> <li> <a href="#" class="mh-icon" title="Esconder menu horizontal" onclick="hideMenuHorizontal();"> <img title="Esconder menu horizontal" id="imgHideMenu" src="http://placehold.it/200/ff0000" width="16" height="16"> </a> </li> </ul> </div> <div id="div-mh-ico"> <ul id="ul-icone-mh" class="icones"> <li> <a href="#" class="mh-icon" title="Esconder menu horizontal" onclick="hideMenuHorizontal();"> <img title="Exibir menu horizontal" id="imgHideMenu" src="http://placehold.it/200/990000" width="16" height="16"> </a> </li> </ul> </div> 

Here I've increased the images to 50x50px from 16x16px to make them a bit easier to see but the principle is just the same. 在这里,我将图像从16x16px增加到了50x50px,以使它们更易于查看,但原理是相同的。

For the two images referenced by the HTML, I've used two blocks that are different shades of red. 对于HTML引用的两个图像,我使用了两个深浅不同的红色块。

For the two CSS overlay images, for simplicity I've used a linear gradient making a block that's the height of the image and twice the width. 对于两个CSS覆盖图像,为简单起见,我使用了线性渐变来制作图像的高度和宽度的两倍的块。 The left half is green and the right half blue. 左半部分为绿色,右半部分为蓝色。 You would use a sprite for the two images you want to display. 您将为要显示的两个图像使用一个精灵 The left half of the sprite would contain the "Esconder ..." replacement image and the right half of the sprite would contain the "Exibir ..." replacement image. Sprite的左半部分将包含“ Esconder ...”替换图像,Sprite的右半部分将包含“ Exibir ...”替换图像。

I've also shown both cases together rather than switching between them on click, again for simplicity. 为了简化起见,我还展示了这两种情况,而不是在单击时在它们之间切换。

The idea is that the left margin of the image is made negative to shift it out of the a element. 想法是使图像的左边缘变为负值,以将其移出a元素。 The pseudo element that follows contains the sprite and is shifted into that space, either by the width of the image, or twice the width of the image to show different contents for the two cases. 后面的伪元素包含精灵,并通过图像的宽度或图像宽度的两倍移入该空间,以显示两种情况下的不同内容。

Hence we get a green box for the "Esconder ..." case and a blue box for the "Exibir ..." case. 因此,对于“ Esconder ...”案例,我们得到了一个绿色框,对于“ Exibir ...”案例,我们得到了一个蓝色框。

It's not very clear what you're trying to accomplish, but if you're trying to change the anchor based on the image, this is simply not possible using CSS alone. 尚不清楚要完成的任务,但是如果要基于图像更改锚点,仅使用CSS根本不可能做到这一点。

You can target child elements based on their parents, but you can not target parent elements based on their children in CSS (currently). 您可以基于父元素来定位子元素,但不能基于CSS中子对象来定位父元素(当前)。

The only way to do this would be to affect how the HTML renders the two options, or using Javascript. 唯一的方法是影响HTML呈现这两个选项的方式,或者使用Javascript。

There are a few different suggested specs for such a selector, but none have yet been implemented. 对于这种选择器,有一些不同的建议规格,但尚未实现。

Since the img tag is what has the dynamic title, that is the only thing you will be able to target with your CSS. 由于img标签具有动态标题,因此这是您可以使用CSS定位的唯一对象。 If you cannot accomplish your task by targeting the img then it can't be done within the constraints you stated. 如果您无法通过定位img来完成任务,那么就无法在您指定的限制内完成任务。

Selectors: https://www.w3.org/TR/CSS2/selector.html 选择器: https : //www.w3.org/TR/CSS2/selector.html

Support for the proposed spec for a parent selector: https://caniuse.com/#feat=css-has 支持针对父选择器的建议规范https : //caniuse.com/#feat=css-has

So if I understand correctly, then the title is "esconder", you want to hide the default image and inject your own. 因此,如果我理解正确,则标题为“ esconder”,您想隐藏默认图像并插入自己的图像。 And otherwise you want to show the original (when the title is "exibir".) 否则,您想显示原始图片(标题为“ exibir”时)。

You have correctly identified how you would target the a tag based on the title: a[title="Esconder menu horizontal"] . 你已经正确识别你将如何针对a基于标题标记: a[title="Esconder menu horizontal"] What you then need to do is only exclude the image when it is inside of this tag, and then replace it with your own image. 然后,您需要做的就是仅将图像排除在此标签内时将其排除,然后将其替换为您自己的图像。 You then also need to give it an explicit size, and declare the a tag which now directly has the background image with some size. 然后,您还需要为其指定一个明确的尺寸,并声明a标签, a标签现在直接具有一定尺寸的背景图像。 Like so: 像这样:

a[title="Esconder menu horizontal"] img { display: none; }
a[title="Esconder menu horizontal"] {
  box-sizing: border-box;
  background-image: url(../images/ico_hidemhc.png);
  background-size: 16px;
  background-repeat: no-repeat;
  height: 16px;
  width: 16px;
  display:inline-block;
}

You will notice I added a few lines to your existing styling: 您会注意到我在您现有的样式中添加了几行:

  • display:inline-block tells the browser that this element should follow the flow like an inline element, but should have block-type semantics. display:inline-block告诉浏览器该元素应像inline元素一样遵循流程,但应具有块类型的语义。 By default, an anchor tag is an inline element, which means it doesn't have explicit size or width -- just what is enforced by its children. 默认情况下,anchor标签是一个内联元素,这意味着它没有明确的大小或宽度-只是其子元素强制执行的内容。 Since you've delcared that the child is not to be seen, the anchor tag effectively collapses to be of 0x0 size. 由于您已经暗示了看不到孩子,因此锚标记实际上会折叠为0x0大小。
  • height:16px; width:16px height:16px; width:16px tells the browser the size you want for this image. height:16px; width:16px告诉浏览器您想要此图像的大小。 I guessed at these dimensions based on the background-size property you had set. 我根据您设置的background-size属性猜测了这些尺寸。 Since we've told the browser using the display property that this element has explicit size, we now tell it what that size is. 由于我们已经使用display属性告知浏览器此元素具有明确的大小,因此我们现在告诉它该大小是多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM