您好我想使用Jquery从
<div class="profile-tracker" style="width: 577px; height: 404px; position: absolute; top: -2px; left: -2px; z-index: 290;"></div>
答案 0 :(得分:2)
如果您要查找height
属性中指定的style
值,请使用reduce
和split
var styleMap = style.split( ";" ).reduce( ( a, c ) => ( d = c.split( ":" ), a[d[0].trim()] = String(d[1]).trim(), a ), {}); //get style map
<强>演示强>
//console.log( $( ".profile-tracker" ).attr( "style" ) );
var style = $( ".profile-tracker" ).attr( "style" ); //get style attribute value
var styleMap = style.split( ";" ).reduce( ( a, c ) => ( d = c.split( ":" ), a[d[0].trim()] = String(d[1]).trim(), a ), {}); //get style map
console.log( styleMap[ "height" ] ); //get height value specified
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="profile-tracker" style="width: 577px; height: 404px; position: absolute; top: -2px; left: -2px; z-index: 290;"></div>
如果样式中还包含url
,那么
//console.log( $( ".profile-tracker" ).attr( "style" ) );
var style = $( ".profile-tracker" ).attr( "style" ); //get style attribute value
var styleMap = style.split( ";" ).reduce( ( a, c ) => ( d = c.split( ":" ), a[d[0].trim()] = String(d[1]).trim(), a ), {}); //get style map
console.log( styleMap[ "height" ] ); //get height value specified
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="profile-tracker" style="no-repeat url(';../../media/examples/lizard.png') ;width: 577px; height: 404px; position: absolute; top: -2px; left: -2px; z-index: 290;"></div>
根据spec,style属性的语法如下
声明列表
:S *声明? [';' S *声明? ] *
注意强>
答案 1 :(得分:0)