简体   繁体   English

根据类别设置元素宽度

[英]Set element width based on class

I'm trying to set the width of an image depending on the class of seperate div using jQuery. 我正在尝试使用jQuery根据单独div的类设置图像的宽度。 I thought I could use an if statement but it isn't working. 我以为我可以使用if语句,但是它不起作用。

$(document).ready(function() {
  if ($('#map-container').hasClass('zoom-100, zoom-130, zoom-150')) {
    $('#marker').css("width", 70);
  } else if ($('#map-container').hasClass('zoom-70, zoom-80')) {
    $('#marker').css("width", 40);
  } else {
    $('#marker').css("width", 25);
  }
});

If #marker is a child of #map-container, you dont need jquery - just use css and target the div accordingly. 如果#marker是#map-container的子级 ,则不需要jquery-只需使用css并相应地将div作为目标。 First set the base size and then set the size according to the parents classes. 首先设置基本尺寸,然后根据父类设置尺寸。

 #marker {
   width: 25px;
 }

 #map-container.zoom-70 #marker,
 #map-container.zoom-80 #marker {
  width: 40px;
 }

 #map-container.zoom-100 #marker,
 #map-container.zoom-130 #marker,
 #map-container.zoom-150 #marker {
  width: 70px;
 }

If #marker is a sibling of #map-container, you still dont need jquery - just use css and target the div via the general sibling combinator " ~ ". 如果#marker是#map-container的同级对象 ,则仍然不需要jquery-只需使用css并通过通用的同级组合器 ”定位div。 First set the base size and then set the size according to the siblings classes. 首先设置基本大小,然后根据同级类设置大小。

 #marker {
   width: 25px;
 }

 #map-container.zoom-70 ~ #marker,
 #map-container.zoom-80 ~ #marker {
  width: 40px;
 }

 #map-container.zoom-100 ~ #marker,
 #map-container.zoom-130 ~ #marker,
 #map-container.zoom-150 ~ #marker {
  width: 70px;
 }

If you absolutely, positivly have to use jquery - then you can use a switch statement which is always better than multiple if statements. 如果绝对需要,肯定要使用jquery-那么您可以使用switch语句 ,该语句总是比多个if语句更好。

Note that multiple statements can be blocked together and then the default statement occurs if none of the preceding statements evaluate to true. 请注意,可以将多个语句一起阻塞,然后,如果以上任何一个语句都不为true,则默认语句将出现。

The following snippet demonstrates the different switch statements; 以下代码段演示了不同的switch语句;

 updateDiv(); $('#class-selector').change(function(){ document.querySelector('#map-container').className = 'zoom-' + $(this).val(); updateDiv(); }) function updateDiv() { switch(true) { case $('#map-container').hasClass('zoom-150'): case $('#map-container').hasClass('zoom-130'): case $('#map-container').hasClass('zoom-100'): $('#marker').css("width", 70).text('70px'); break; case $('#map-container').hasClass('zoom-80'): case $('#map-container').hasClass('zoom-70'): $('#marker').css("width", 40).text('40px'); break; default: $('#marker').css("width", 25).text('25'); } } 
 #map-container { border: solid 1px red; padding: 15px; } #marker { background: blue; color: white; text-align: center } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="map-container" class="zoom-100"> <p>This div with the red border is #map-container and the the following blue div is #marker. Changing the select list below will apply the different class to #map-container and will change the blue div accordingly.</p> <label>Select a different size to apply to the div</label> <select id="class-selector"> <option value="">None</option> <option value="70">zoom-70</option> <option value="80">zoom-80</option> <option value="100" selected>zoom-100</option> <option value="130">zoom-130</option> <option value="150">zoom-150</option> </select> <hr/> <div id="marker"></div> </div> 

使用jQuery is()方法

  $('#map-container').is('.zoom-100, .zoom-130, .zoom-150')
$(document).ready(function(){
    if ($('#map-container.zoom-100.zoom-130.zoom-150').length) {
        $('#marker').css("width", 70);
    }
    else if ($('#map-container.zoom-70.zoom-80').length) {
        $('#marker').css("width", 40);
    }
    else {
        $('#marker').css("width", 25);
    }
});

use js logic operators: 使用js逻辑运算符:

if( $('#myID').hasClass('class1') && $('#myID').hasClass('class2') ){
  /* do something */
}

As others mantioned, you can do this by css depending on your html. 正如其他人提到的那样,您可以根据您的html通过css做到这一点。 But if for some reason you want to use jQuery you can do it be using an or for each class. 但是,如果由于某种原因要使用jQuery,可以对每个类使用或来实现。

 var $mapCon = $('#map-container') if ($mapCon.hasClass('zoom-100') || $mapCon.hasClass('zoom-130') || $mapCon.hasClass('zoom-150')) { $('#marker').css("background-color", "pink"); } else if ($mapCon.hasClass('zoom-70') || $mapCon.hasClass('zoom-80')) { $('#marker').css("background-color", "lightblue"); } else { $('#marker').css("background-color", "yellow"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="map-container" class="zoom-70">container</div> <div id="marker">test</div> 

hasClass() uses only one argument. hasClass()仅使用一个参数。 You could find it on JQuery official doc . 您可以在JQuery官方文档上找到它。

You will have to implement multiple conditions. 您将必须实现多个条件。

.hasClass only accepts one parameter for checking for a single class. .hasClass仅接受一个参数来检查单个类。 An alternative is to use the jQuery is function . 一种替代方法是使用jQuery is function

 if ($("#map-container").is(".zoom-100,.zoom-130,.zoom-150")) {
   $('#marker').css("width", 70);
 }

It doesn't make too much sense to use jQuery here. 在这里使用jQuery没有太大的意义。 CSS would make much more sense, if there is not a special reason not mentioned in your example. 如果没有在您的示例中未提及的特殊原因,CSS会更有意义。

do not take multiple class names separated by coma in has calss method . 不要在hassss方法中使用多个用逗号分隔的类名。 the solution is bellow code 解决方案是下面的代码

  $(document).ready(function () { //if ($('#map-container').hasClass('zoom-100, zoom-130, zoom-150')) { if ($('#map-container').hasClass('zoom-150')) { $('#marker').css("width", 270); } else if ($('#map-container').hasClass('zoom-130')) { $('#marker').css("width", 340); } else if ($('#map-container').hasClass('zoom-70')) { $('#marker').css("width", 40); } else { $('#marker').css("width", 25); } }); 

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

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