Javascript 更改选项卡颜色

Javascript 更改选项卡颜色,javascript,html,css,Javascript,Html,Css,我使用javascript选项卡根据选项卡选择显示内容。它工作得很好。我只想更改选项卡的背景色。我需要在四个单独的选项卡上显示4种不同的背景色。内联css不起作用,因为它是从javascript文件调用的。我从这里得到了密码: 代码: Html: 以上是所有选项卡的默认背景色 Javascript: if (!t.headingText) { /* Title was not found (or is blank) so automatically generate a num

我使用javascript选项卡根据选项卡选择显示内容。它工作得很好。我只想更改选项卡的背景色。我需要在四个单独的选项卡上显示4种不同的背景色。内联css不起作用,因为它是从javascript文件调用的。我从这里得到了密码:

代码:

Html:

以上是所有选项卡的默认背景色

Javascript:

  if (!t.headingText) {
  /* Title was not found (or is blank) so automatically generate a
     number for the tab.
  */
  t.headingText = i + 1;
 }

 /* Create a list element for the tab */
 DOM_li = document.createElement("li");

 /* Save a reference to this list item so we can later change it to
   the "active" class */
 t.li = DOM_li;

 /* Create a link to activate the tab */
 DOM_a = document.createElement("a");
 DOM_a.appendChild(document.createTextNode(t.headingText));
 DOM_a.href = "javascript:void(null);";
 DOM_a.title = t.headingText;
 DOM_a.onclick = this.navClick;

 /* Add some properties to the link so we can identify which tab
   was clicked. Later the navClick method will need this.
 */
 DOM_a.tabber = this;
 DOM_a.tabberIndex = i;

 /* Do we need to add an id to DOM_a? */
  if (this.addLinkId && this.linkIdFormat) {

  /* Determine the id name */
  aId = this.linkIdFormat;
  aId = aId.replace(/<tabberid>/gi, this.id);
  aId = aId.replace(/<tabnumberzero>/gi, i);
  aId = aId.replace(/<tabnumberone>/gi, i+1);
  aId = aId.replace(/<tabtitle>/gi, t.headingText.replace(/[^a-zA-Z0-9\-]/gi, ''));

  DOM_a.id = aId;
 }

 /* Add the link to the list element */
 DOM_li.appendChild(DOM_a);

 /* Add the list element to the list */
 DOM_ul.appendChild(DOM_li);
 }
试着这样做:

CSS:

!!重要信息:这将覆盖正在应用的任何属性


希望这有帮助

最好使用更高特异性的选择器来覆盖默认颜色!重要的,

试一试

您可以按如下方式为每个选项卡着色:


etc

请更新您的完整代码Hi Karthick,我从这里获得代码:!重要的是骇客。如果可能的话,不要使用这个。尝试切换将改变颜色的类名称,而不是应用内嵌w/JS的样式。可能是覆盖选定的类?嗨,Tilwin,谢谢你的帮助。现在它显示的是单独的颜色。但是活动选项卡背景颜色重叠:ul.tabbernav li.tabberactive a{background color:fff;border左上半径:1em;border右上半径:1em;border bottom:1px solid fff;}嘿,感谢我提供的活动选项卡!重要的现在它工作得很好。
  ul.tabbernav li a {
  background: #DDE;
  }
  if (!t.headingText) {
  /* Title was not found (or is blank) so automatically generate a
     number for the tab.
  */
  t.headingText = i + 1;
 }

 /* Create a list element for the tab */
 DOM_li = document.createElement("li");

 /* Save a reference to this list item so we can later change it to
   the "active" class */
 t.li = DOM_li;

 /* Create a link to activate the tab */
 DOM_a = document.createElement("a");
 DOM_a.appendChild(document.createTextNode(t.headingText));
 DOM_a.href = "javascript:void(null);";
 DOM_a.title = t.headingText;
 DOM_a.onclick = this.navClick;

 /* Add some properties to the link so we can identify which tab
   was clicked. Later the navClick method will need this.
 */
 DOM_a.tabber = this;
 DOM_a.tabberIndex = i;

 /* Do we need to add an id to DOM_a? */
  if (this.addLinkId && this.linkIdFormat) {

  /* Determine the id name */
  aId = this.linkIdFormat;
  aId = aId.replace(/<tabberid>/gi, this.id);
  aId = aId.replace(/<tabnumberzero>/gi, i);
  aId = aId.replace(/<tabnumberone>/gi, i+1);
  aId = aId.replace(/<tabtitle>/gi, t.headingText.replace(/[^a-zA-Z0-9\-]/gi, ''));

  DOM_a.id = aId;
 }

 /* Add the link to the list element */
 DOM_li.appendChild(DOM_a);

 /* Add the list element to the list */
 DOM_ul.appendChild(DOM_li);
 }
 ul.tabbernav li a {
  background: #DDE !important;
  }
.tabberlive ul.tabbernav li a {
 background: #DDE;
}
.tabberlive ul.tabbernav li:nth-child(1) a {
 background: cyan; /* color for 1st tab */
}
.tabberlive ul.tabbernav li:nth-child(2) a {
background: silver; /* color for 2nd tab */
}
.tabberlive ul.tabbernav li:nth-child(3) a {
background: yellow; /* color for 3rd tab */
}
.tabberlive ul.tabbernav li:nth-child(4) a {
background: cyan; /* color for 4th tab */
}