简体   繁体   English

如何更改不是“(obj)”元素的样式

[英]How to change style of elements that aren't “(obj)”

I have multiple items in a row that I want to easily change the style of the border based on my selection. 我连续多个项目,我想根据我的选择轻松更改边框的样式。

Here is the HTML of just the row and some of the items in it: 这是行中的一些HTML以及其中的一些项目:

<div class="items">
              <ul>
                <li class="item-hold">
                  <span class="item icon64x64">
                    <img class="item-img icon64x64" src="css/img/3001.png" alt="Abyssal Scepter" id="as">
                  </span>
                </li>
                <li class="item-hold">
                  <span class="item icon64x64">
                    <img class="item-img icon64x64" src="css/img/3020.png" alt="Sorcerer's Shoes" id="ss">
                  </span>
                </li>
                <li class="item-hold">
                  <span class="item icon64x64">
                    <img class="item-img icon64x64" src="css/img/3025.png" alt="Iceborn Gauntlet" id="ig">
                  </span>
                </li>
             </ul>
       </div>

I have tried to do if !(obj).style.... . 我试过这样做!(obj).style.... However that won't work and I cannot find any solutions anywhere. 然而,这将无法工作,我无法在任何地方找到任何解决方案。

I Know how to do this with states and cases. 我知道如何用状态和案例来做到这一点。 However, I didn't want my JS to be a few 100 lines long. 但是,我不希望我的JS长达100行。

So here is my js 所以这是我的js

var as = document.getElementById('as');
var ss = document.getElementById('ss');
var ig = document.getElementById('ig');

as.addEventListener('click', function() {
  ItemDisc('as');
});
ss.addEventListener('click', function() {
  ItemDisc('ss');
});
ig.addEventListener('click', function() {
  ItemDisc('ig');
});

function ItemDisc(obj) {
  var change = document.getElementById(obj);
  var changeback = document.getElementById(!obj);
  change.style.border = "5px solid blue";
  for(!obj) {
    changeback.style.border = "5px solid blue";
  }
}

This is a basic demo that could be improved upon. 这是一个可以改进的基本演示。

The main idea is to loop through all the items and "reset" them to their default state. 主要思想是遍历所有项目并将其“重置”为默认状态。 Then apply the selection style to the selected element. 然后将选择样式应用于所选元素。

<div class="items">
  <ul>
    <li>
      <img src="https://placehold.it/50x50&text=01">
    </li>
    <li>
      <img src="https://placehold.it/50x50&text=02">
    </li>
    <li>
      <img src="https://placehold.it/50x50&text=03">
    </li>
  </ul>
</div>
ul, li {
  list-style: none;
  margin: 0;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 1rem;
}
.highlight {
  border: 2px solid red;
}
// Get all items.
var items = document.querySelectorAll( '.items li' );

// Adding/removing selection style via a CSS class.
function addHighlight() { 

  // Loop through all items and remove the selection class.
  for ( var i = 0, len = items.length; i < len; i++ ) {
    items[i].className = items[i].className.replace( 'highlight', '' );
  }

  // Add selection class to selected item.
  this.className += ' highlight';

}

// Add click event handler to items.
function addEventListener( items, event, listener ) {
    for ( var i = 0, len = items.length; i < len; i++ ) {
        items[ i ].addEventListener( event, listener );
    }
}

addEventListener( items, 'click', addHighlight ); 

Demo JSFiddle . 演示JSFiddle

I dont understand what you are trying to do, so I can't recreate something in its entirety. 我不明白你要做什么,所以我不能完全重建一些东西。

I can point you in the right direction though. 我可以指出你正确的方向。

Your problem is on line 你的问题在线了

var changeback = document.getElementById(!obj)

!obj is being resolved to the Boolean 'false', and not the element you are selecting. !obj被解析为布尔'false',而不是你选择的元素。

Furthermore, you are using 'for', when you should be using 'if' For is creating loops, and 'if' is for conditions 此外,你正在使用'for',当你应该使用'if'For是创建循环,'if'是条件

if(!obj) {
   changeback.style.border = "5px solid blue";
}

Also, the border color is exactly the same. 此外,边框颜色完全相同。

I think it is possible to achieve what you want by changing your ItemDisc(obj) function to this. 我认为通过将ItemDisc(obj)函数更改为此可以实现您想要的功能。

function ItemDisc(obj){
    element = document.getElementById(obj);
    if(element.classList.contains('active')){
        element.className += " active";
        element.style.border = "5px solid blue";
    } else {
        element.className = "";
        // Careful, because this will remove all classes from your element.
    }
}

Just wanted to say... This is without jQuery, also, you can make it easier by adding styles to your css class 'active' which included borders. 只是想说...这是没有jQuery的,你也可以通过添加样式到你的css类'active'(包括边框)来简化它。

} }

You can also use this as your JS: 您也可以将它用作JS:

    var imgs = document.getElementsByClassName('item-img');

    for(i=0; i<imgs.length; i++) {
        imgs[i].addEventListener('click', function(){
            for (i=0; i<imgs.length; i++)
                imgs[i].style.border='1px solid blue';
            this.style.border = '1px solid red';
        });
    }

This is very simple to do with jQuery. 这与jQuery非常简单。 I'd recommend learning jQuery because it will familiarize you with both css-selectors and JavaScript. 我建议学习jQuery,因为它会让你熟悉css选择器和JavaScript。 Here's a boilerplate to get you started, please forgive any typos: 这是一个帮助您入门的样板,请原谅任何错别字:

    <style>
        .active{border:5px solid #0000FF;}
    </style>

    $(".item-img").click(function(){
        .each(".item-img"){
            myFunction( $(this).attr("id") );
        }
    });

    function myFunction(theID){
        if( $(this).attr("id") == theID ){
            $(this).addClass("active");
        }else{
            $(this).removeClass("active");
        }
    }

You will want to load jQuery in your html. 您将需要在您的HTML中加载jQuery。 Also, you'll need to wrap the js above in: 此外,您需要将上面的j包装在:

    $(document).ready(function(){/*above code goes here*/});

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

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