css属性

shstlldc  于 2021-09-13  发布在  Java
关注(0)|答案(3)|浏览(271)

下面是几段代码

$('.catalogbutton').on('click', function(event) {
  console.log('Before');
  $('#dropdownwindow').style.marginRight = "40%";
  $('#dropdownwindow').style.display = "block";
  $('#dropdownwindow').style.height = "100%";
  console.log('After');

})

# pageHeader {

  grid-area: header;
  width: 100%;
  /*
    display: flex;
    justify-content: space-between;
    align-items: center;
    */
}

# pageHeader .headerinsides {

  padding-left: 65px;
  padding-right: 65px;
  padding-top: 10px;
  padding-bottom: 10px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  font: 18px "PT Sans", sans-serif;
  /*align-items: center;*/
}

.icon {
  align-self: center;
  /*padding-left: 74px;*/
  /*padding-right: 74px;*/
}

# pageHeader .labelforicon {

  padding-left: 10px;
  /*padding-right: auto;*/
  /*width: 1200px;*/
  align-self: center;
  /*flex: 34px;*/
  /*border-bottom: 2.5px dotted black;*/
  font: 18px "PT Sans", sans-serif;
  /*white-space: pre;*/
}

.flexcitylink {
  /*margin-left: 74px;*/
  display: flex;
  flex: 60px;
  /*font: 18px, PT Sans,sans-serif;*/
  width: 100%;
}

.flexcitylinkwrapper {
  display: flex;
}

/*
.headerparamswrapper {
    display: flex;
    margin-left: auto;
}

* /

.intermediatespace {
  margin-left: auto;
  flex: 30%;
}

.telephonenumber {
  margin-left: auto;
  flex: 30%;
}

.addressandworkschedule {
  margin-left: auto;
  flex: 20%;
}

.payment {
  margin-left: auto;
  flex: 10%;
}

.delivery {
  margin-left: auto;
  flex: 10%;
}

# pageHeader .headerinsidessecondrow {

  padding-left: 0px;
  padding-right: 0px;
  width: 100%;
  height: 57px;
  display: flex;
  /*justify-content: space-between;*/
  align-items: center;
  font: 16px "PT Sans", sans-serif;
}

.catalogbutton {
  margin-left: auto;
  margin-right: auto;
  flex: 22.5%;
  height: 41px;
}

.betweenspace {
  flex: 7.5%;
}

.inputsearch {
  margin-left: auto;
  flex: 80%;
  height: 41px;
  /*border-bottom-width: 0px;*/
  /*height: 32px;*/
}

.searchbutton {
  margin-left: auto;
  flex: 20%;
  height: 41px;
}

.findinfo {
  flex: 40%;
  display: flex;
  justify-content: stretch;
}

.secondbetweenspace {
  flex: 10%;
}

.loginandsignup {
  margin-left: auto;
  /*margin-right: 0px;*/
  display: flex;
  align-items: center;
  flex: 10%;
  display: flex;
  justify-content: flex-start;
  align-self: center;
}

.basket {
  margin-left: auto;
  /*margin-right: 0px;*/
  display: flex;
  align-items: center;
  flex: 10%;
  display: flex;
  justify-content: flex-start;
  align-self: center;
}

# dropdownwindow {

  display: none;
  left: 0;
  margin-right: 100%;
  height: 100%;
}
<div id="dropdownwindow">
</div>
<header id="pageHeader">
  <div class="headerinsides">
    <span class="flexcitylinkwrapper">
        <a href="#" class="flexcitylink">
                <span class="icon">
                        <svg aria-hidden="true" class="Sg" width="16" height="16" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M8.057 8a2.012 2.012 0 01-2.019-2c0-1.103.906-2 2.02-2 1.113 0 2.018.897 2.018 2s-.905 2-2.019 2zm0-5C6.387 3 5.03 4.346 5.03 6s1.358 3 3.028 3 3.029-1.346 3.029-3-1.359-3-3.029-3zm0 11.777C6.852 13.5 3.01 9.177 3.01 6c0-2.757 2.264-5 5.047-5 2.784 0 5.047 2.243 5.047 5 0 3.174-3.841 7.5-5.047 8.777zM8.057 0C4.717 0 2 2.691 2 6c0 4.159 5.468 9.623 5.7 9.854a.509.509 0 00.714 0c.233-.23 5.7-5.695 5.7-9.854 0-3.309-2.716-6-6.057-6z" fill="currentColor"></path></svg>
                </span>
    <span class="labelforicon">
                 Краснодар
                </span>
    </a>
    </span>
    <span class="intermediatespace">
                </span>
    <span class="telephonenumber">
                8 (800) 600 3900 (круглосуточно)
                </span>
    <span class="addressandworkschedule">
                    Адрес и график работы
                </span>
    <span class="payment">
                    Оплата
                </span>
    <span class="delivery">
                    Доставка
                </span>
  </div>
  <div class="headerinsidessecondrow">
    <button class="catalogbutton">Каталог товаров</button>

    <span class="betweenspace">
</span>

    <span class="findinfo">
        <input placeholder="Поиск среди 10000 товаров" class="inputsearch"></input>
        <button class="searchbutton">Найти</button>
</span>

    <span class="secondbetweenspace">
</span>

    <span class="loginandsignup">
    <span class="loginsignupimg">
        <img src="../images/header_profile-icon.png">
    </span>
    <span class="loginsignuptext">
        Вход и регистрация
    </span>
    </span>

    <span class="basket">
    <span class="loginsignupimg">
        <img src="../images/header_cart-icon.png">
    </span>
    <span class="loginsignuptext">
        Корзина
    </span>
    </span>
  </div>

因此,当按钮与 .catalogbutton 类,则 #dropdownwindow 块不显示。告诉我如何实现块显示。给出错误消息

wfauudbj

wfauudbj1#

要更改元素的样式,这是jquery的api

$('selector').css({
  marginRight: '40%', 
  display: 'block',
  height: '100%'
});

要了解发生此错误的原因,您可以登录 $('selector') 看到了吗,没有一个属性叫做 style 登录语句

7eumitmz

7eumitmz2#

因为您使用的是jquery,所以最好对元素使用css()方法。检查下面修改的代码段。它的功能很好,只是风格多一点。

$(document).ready(function() {
      $('.catalogbutton').on('click', function(event) {
          $('#dropdownwindow').css('marginRight', "40%");
          $('#dropdownwindow').css('display' ,"block");
          $('#dropdownwindow').css('height', "100%");
          console.log('After');

        })
      });

# pageHeader {

  grid-area: header;
  width: 100%;
  /*
    display: flex;
    justify-content: space-between;
    align-items: center;
    */
}

# pageHeader .headerinsides {

  padding-left: 65px;
  padding-right: 65px;
  padding-top: 10px;
  padding-bottom: 10px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  font: 18px "PT Sans", sans-serif;
  /*align-items: center;*/
}

.icon {
  align-self: center;
  /*padding-left: 74px;*/
  /*padding-right: 74px;*/
}

# pageHeader .labelforicon {

  padding-left: 10px;
  /*padding-right: auto;*/
  /*width: 1200px;*/
  align-self: center;
  /*flex: 34px;*/
  /*border-bottom: 2.5px dotted black;*/
  font: 18px "PT Sans", sans-serif;
  /*white-space: pre;*/
}

.flexcitylink {
  /*margin-left: 74px;*/
  display: flex;
  flex: 60px;
  /*font: 18px, PT Sans,sans-serif;*/
  width: 100%;
}

.flexcitylinkwrapper {
  display: flex;
}

/*
.headerparamswrapper {
    display: flex;
    margin-left: auto;
}

* /

.intermediatespace {
  margin-left: auto;
  flex: 30%;
}

.telephonenumber {
  margin-left: auto;
  flex: 30%;
}

.addressandworkschedule {
  margin-left: auto;
  flex: 20%;
}

.payment {
  margin-left: auto;
  flex: 10%;
}

.delivery {
  margin-left: auto;
  flex: 10%;
}

# pageHeader .headerinsidessecondrow {

  padding-left: 0px;
  padding-right: 0px;
  width: 100%;
  height: 57px;
  display: flex;
  /*justify-content: space-between;*/
  align-items: center;
  font: 16px "PT Sans", sans-serif;
}

.catalogbutton {
  margin-left: auto;
  margin-right: auto;
  flex: 22.5%;
  height: 41px;
}

.betweenspace {
  flex: 7.5%;
}

.inputsearch {
  margin-left: auto;
  flex: 80%;
  height: 41px;
  /*border-bottom-width: 0px;*/
  /*height: 32px;*/
}

.searchbutton {
  margin-left: auto;
  flex: 20%;
  height: 41px;
}

.findinfo {
  flex: 40%;
  display: flex;
  justify-content: stretch;
}

.secondbetweenspace {
  flex: 10%;
}

.loginandsignup {
  margin-left: auto;
  /*margin-right: 0px;*/
  display: flex;
  align-items: center;
  flex: 10%;
  display: flex;
  justify-content: flex-start;
  align-self: center;
}

.basket {
  margin-left: auto;
  /*margin-right: 0px;*/
  display: flex;
  align-items: center;
  flex: 10%;
  display: flex;
  justify-content: flex-start;
  align-self: center;
}

# dropdownwindow {

  display: none;
  left: 0;
  margin-right: 100%;
  height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dropdownwindow">
<p>Drop Down</p>
</div>
<header id="pageHeader">
  <div class="headerinsides">
    <span class="flexcitylinkwrapper">
        <a href="#" class="flexcitylink">
                <span class="icon">
                        <svg aria-hidden="true" class="Sg" width="16" height="16" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M8.057 8a2.012 2.012 0 01-2.019-2c0-1.103.906-2 2.02-2 1.113 0 2.018.897 2.018 2s-.905 2-2.019 2zm0-5C6.387 3 5.03 4.346 5.03 6s1.358 3 3.028 3 3.029-1.346 3.029-3-1.359-3-3.029-3zm0 11.777C6.852 13.5 3.01 9.177 3.01 6c0-2.757 2.264-5 5.047-5 2.784 0 5.047 2.243 5.047 5 0 3.174-3.841 7.5-5.047 8.777zM8.057 0C4.717 0 2 2.691 2 6c0 4.159 5.468 9.623 5.7 9.854a.509.509 0 00.714 0c.233-.23 5.7-5.695 5.7-9.854 0-3.309-2.716-6-6.057-6z" fill="currentColor"></path></svg>
                </span>
    <span class="labelforicon">
                 Краснодар
                </span>
    </a>
    </span>
    <span class="intermediatespace">
                </span>
    <span class="telephonenumber">
                8 (800) 600 3900 (круглосуточно)
                </span>
    <span class="addressandworkschedule">
                    Адрес и график работы
                </span>
    <span class="payment">
                    Оплата
                </span>
    <span class="delivery">
                    Доставка
                </span>
  </div>
  <div class="headerinsidessecondrow">
    <button class="catalogbutton">Каталог товаров</button>

    <span class="betweenspace">
</span>

    <span class="findinfo">
        <input placeholder="Поиск среди 10000 товаров" class="inputsearch"></input>
        <button class="searchbutton">Найти</button>
</span>

    <span class="secondbetweenspace">
</span>

    <span class="loginandsignup">
    <span class="loginsignupimg">
        <img src="../images/header_profile-icon.png">
    </span>
    <span class="loginsignuptext">
        Вход и регистрация
    </span>
    </span>

    <span class="basket">
    <span class="loginsignupimg">
        <img src="../images/header_cart-icon.png">
    </span>
    <span class="loginsignuptext">
        Корзина
    </span>
    </span>
  </div>

希望这能解决你的问题!

vyswwuz2

vyswwuz23#

你在混东西。 style 是dom元素的属性,并且 $() 返回完全不同类型的对象(特定于jquery)。样式不是该对象的属性,因此未定义。
使用 .css() 特定于jquery的方法。
语法: css("propertyname","value") 更改您的陈述,如下所示:

$('#dropdownWindow).css("marginRight","40%");

相关问题