Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 获取子div数据属性的高度,然后设置父属性的高度_Jquery_Css_Height - Fatal编程技术网

Jquery 获取子div数据属性的高度,然后设置父属性的高度

Jquery 获取子div数据属性的高度,然后设置父属性的高度,jquery,css,height,Jquery,Css,Height,html如下所示 <div id="carousel4up"> <div class="owl-carousel" data-composer='carousel4up' data-config='{ "height" : 350, "other": false }' ... </div> </div> 我不知道如何通过数组来定位,但是我不知道如何获取高度的值 改变这个 var o

html如下所示

  <div id="carousel4up"> 
       <div class="owl-carousel" data-composer='carousel4up' data-config='{
       "height" : 350, "other": false }'
        ...
        </div>
  </div>
我不知道如何通过数组来定位,但是我不知道如何获取高度的值

改变这个

 var owlHeight = $(".owl-carousel").attr(data-config); 
对此

 var owlHeight = $(".owl-carousel").data('config'); 
 //or .attr('data-config'); 

一个问题是,您需要使用单引号:

data-config='{"height" : 350, "other": false }';
要将属性设置为JS数组,请执行以下操作:

    var owlHeight = $.parseJSON($(".owl-carousel").attr('data-config'));
    //or the non-jQuery version JSON.parse($(".owl-carousel").attr('data-config')); 
那么就这样称呼它:

$("#carousel4up").height(owlHeight['height']);

如果您提供的数据是格式的,则可以执行以下操作:

//如果希望javascript中的数据配置值发生更改,请使用attr //否则使用.data var config=$carousel4up.owl-carousel.attrdata-config; //parse将JSON字符串转换为javascript对象 var height=JSON.parseconfig.height; $carousel4up.csheight,高度+px; 旋转木马{ 背景:红色; } 我很大,耶!
thnks似乎在我的控制台中工作。将在应用程序中测试并确认。我不明白为什么json必须是单引号though@nolawipetros它不一定是这样的,你是如何得到它的,它可能会弄乱你的html,如果它真的工作了,很可能是这样的buggy@nolawipetros实际上,我想你要么使用单引号,要么转义所有的双引号。单引号比较容易没问题:我很高兴能帮上忙!
$("#carousel4up").height(owlHeight['height']);