简体   繁体   English

CSS inheritance 不工作

[英]CSS inheritance not working

Screenshot 1截图一

在此处输入图像描述

Screenshot 2截图2

根据提示答案修改后

I am currently stuck on a css issue.我目前陷入了 css 问题。 Basically I have defined a style rule like this:基本上我定义了这样的样式规则:

#divMyList tbody tr td{
    cursor:pointer;
    border-right:5px solid white;
    padding:10px;
    width:200px;
}

I'm applying another class named tmenu on my td in the <div> like this:我在<div>中的td上应用另一个名为tmenu的 class,如下所示:

<td class="tmenu"> foo </td>

so that it inherits all the color and other combinations from along with my overridden styles in #divMyList tbody tr td I mentioned above.这样它就继承了我上面提到的#divMyList tbody tr td中覆盖的 styles 的所有颜色和其他组合。 This is working fine for me.这对我来说很好用。

Now, I want to implement the selected style of tmenu to my current <td> element so that when someone clicks on it, it inherits the selected style of tmenu class. The tmenu and its selected styles are defined like this:现在,我想将tmenu的选定样式实现到我当前的<td>元素,以便当有人单击它时,它会继承 tmenu tmenu tmenu选定的 styles 定义如下:

.tmenu {
    width: 100%;
    height: 40px;
    font-size: 14px;
    font-weight: normal;
}

.tmenu ul li {
    /* ..... */
}

.tmenu ul li.selected {
    cursor: default;
}

When I do like this:当我这样做时:

<td class="tmenu selected">foo</td>

it doesn't apply the rules of the selected class to my td element.它不会将所选 class 的规则应用于我的 td 元素。 Any help on what I'm doing wrong.关于我做错了什么的任何帮助。 Do I need another rule mixing all of these in a new class?我是否需要将所有这些混合在一个新的 class 中的另一条规则?

.tmenu ul li.selected { [...] }

Is going to look for an element structured like this: 将寻找一个结构如下的元素:

<elem class="tmenu">
  <ul>
    <li class="selected"> </li>  <!-- This is going to get styled! -->
  </ul>
</elem>

It sounds like what you are looking for is this: 听起来您正在寻找的是这样的:

.tmenu.selected { [...] } 

Keep in mind something needs to apply the selected class to tmenu, and that it won't automatically happen by simply clicking on it. 请记住,需要将selected类应用于tmenu,并且单击它不会自动发生。

the way you have defined your table, your css should look like this 定义表的方式,css应该如下所示

#divMyList tbody tr td{
    cursor:pointer;
    border-right:5px solid white;
    padding:10px;
    width:200px;
}

.topmenu {
    width: 100%;
    height: 40px;
    font-size: 14px;
    font-weight: normal;
}

.topmenu td.selected{
    cursor: default!important;
}

I have put together a fiddle and added a color to show that it is getting styled 我整理了一个小提琴,并添加了一种颜色来表明它正在被样式化

If you are using ASP.Net Forms application try document.getElementById('MainContent_test').innerHTML = carName;如果您使用的是 ASP.Net Forms 应用程序,请尝试document.getElementById('MainContent_test').innerHTML = carName;

If you do an 'Inspect' when you run the application you will see that ASP.Net renders the control with 'MainContent_[your control ID]' as the ID.如果您在运行应用程序时执行“检查”,您将看到 ASP.Net 以“MainContent_[您的控件 ID]”作为 ID 呈现控件。

Once you get the name right it works.一旦你得到正确的名字,它就会起作用。

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

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