【问题标题】:I want to check if my id or class exists in a table | JavaScript我想检查我的 id 或 class 是否存在于表中 | JavaScript
【发布时间】:2019-11-03 22:19:17
【问题描述】:

我有 2 张表,一张带有选择选项,一张带有结果。当我从 select 中选择一个名称时,我会显示一些结果中的图像。当我从选择中专门单击一个名称时,我会更改 CSS 样式。这样做,但我只想在有 id (div_new) 或 class (newA) 的情况下这样做。即,我想从结果中检查我的 id 或 class 是否存在于一张图像中以更改我的样式。 我该怎么做???

使用以下代码,它不会改变两个图像!

var playerAward = document.getElementById("my_select");
var playerAward2 = document.getElementById("my_select2");
var playerSelected = playerAward[playerAward.selectedIndex].value;
var playerSelected2 = playerAward2[playerAward2.selectedIndex].value;

if ((playerSelected == '.Athinodoros' && playerSelected2 == '.Dionisis' && $('#div_new').hasClass(".newA"))
|| (playerSelected == '.Dionisis' && playerSelected2 == '.Athinodoros' && $('#div_new').hasClass(".newA"))) {
      $('.award').css('box-shadow', 'inset 0 0 0 1.5px black');
      $('.award').css('background', 'rgb(255, 250, 240)');
      $('.award').css('background', 'rgba(255, 250, 240, 0.5)');
}

<DIV Class="column">
    <DIV ID="div_new" Class="award Athinodoros Dionisis newA">
        <P><IMG SRC="Team BanList\Ban\Dark Room of Nightmare.jpg"><BR>
        <LABEL Class="overlay">Dark Room of<BR>Nightmare</LABEL></P>
    </DIV>
</DIV>
<DIV Class="column">
    <DIV Class="award Athinodoros Dionisis">
        <P><IMG SRC="Team BanList\Ban\Moisture Creature.jpg"><BR>
        <LABEL Class="overlay"><BR>Moisture Creature</LABEL></P>
    </DIV>
</DIV>

【问题讨论】:

  • 你的问题不清楚。但是,如果您要检查是否存在具有特定 id 的元素,您可以简单地拥有:var playerAward = document.getElementById("my_select");,然后通过 if(playerAward != null &amp;&amp; playerAward != undefined) { } 检查它是否存在
  • 我想检查 newA 是否存在。我的选择是正确的。
  • 那么代码是:if(whatever.classList.contains("newA"))) { }var whatever = document.getElementById('div_new'');
  • 不,它不工作
  • 成功了!!!感谢您的帮助!!!

标签: javascript html jquery class


【解决方案1】:

我之后会这样做,但什么也没做。

var newAward = document.getElementById("div_new");

if ((playerSelected == '.Athinodoros' && playerSelected2 == '.Dionisis' && newAward.classList.contains("newA"))
|| (playerSelected == '.Dionisis' && playerSelected2 == '.Athinodoros' && newAward.classList.contains("newA"))) {
      awardColor = awardColorChange();
    } else if (playerSelected == "" && playerSelected2 == "") {
      awardColor = awardColorChange2();
    }

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 2011-07-25
    • 1970-01-01
    • 2020-04-16
    • 2018-01-13
    • 1970-01-01
    • 2016-08-18
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多