【问题标题】:Can I add a function like an attribute in .setProperty?我可以在 .setProperty 中添加类似属性的函数吗?
【发布时间】:2017-09-17 10:19:00
【问题描述】:

伙计们。我可以在 jQuery .setProperty 中使用属性之类的函数吗?

jQuery(document).ready(function(){
jQuery(".content-wrapper").mouseenter(function(){
    jQuery(this).find('.separator-content-box').each(function(){
        this.style.setProperty( 'width', jQuery(jQuery('.content-wrapper').width() -  jQuery('.heading').width()), 'important' );
        this.style.setProperty( 'border-color', 'red', 'important' );
    });
});

【问题讨论】:

  • 是的!但是在这个“this.style.setProperty('width', jQuery(jQuery('.content-wrapper').width() - jQuery('.heading').width()), 'important ');"

标签: jquery setpropertyactionlistener


【解决方案1】:

你可以试试这个语法。

this[0].style.setProperty( 'width', jQuery(jQuery('.content-wrapper').width() -  jQuery('.heading').width()), 'important' );

this[0].style.setProperty( 'border-color', 'red', 'important' );

this.attr( 'style', 'width: ' + jQuery(jQuery('.content-wrapper').width() -  jQuery('.heading').width()) + 'px !important' );

this.attr( 'style', 'border-color: red !important' );

【讨论】:

    【解决方案2】:

    如有其他问题,请向我们提供与您的案例相关的 CSS 和 HTML 片段。

    在 jQuery 中,我建议使用“css()”函数来为元素添加内联样式。你的代码应该是这样的:

    jQuery(document).ready(function(){
      jQuery(".content-wrapper").mouseenter(function(){
          jQuery(this).find('.separator-content-box').each(function(){
              jQuery(this).css('width',(jQuery('.content-wrapper').width() -  jQuery('.heading').width()));
              jQuery(this).css('border-color','red');
          });
      });
    });
    

    我不知道你的 HTML/CSS 结构,但我做了一个测试:https://jsfiddle.net/97fjbp12/

    注意:要进行计算,您只需添加 () 即可。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2013-05-03
      • 1970-01-01
      • 2013-08-28
      • 2022-06-15
      • 1970-01-01
      • 2010-10-07
      • 2011-03-08
      • 2014-07-20
      相关资源
      最近更新 更多