2014-10-29 2 views

답변

2

예, data-size 속성을 가진 모든 요소를 ​​선택하고 다음과 같이 수행 할 수 있습니다

$(document).ready(function(){ 
 
    $('[data-size]').each(function(){ 
 
    $(this).attr('data-size', $(this).outerHeight() + "px"); 
 
    }); 
 
});
div {border: 1px solid #ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div data-size="this.style.size"> 
 
    <div style="height: 300px;"></div> 
 
</div> 
 
<div data-size="this.style.size"> 
 
    <div style="height: 25px;"></div> 
 
</div> 
 
<div data-size="this.style.size"> 
 
    <div style="height: 50px;"></div> 
 
</div>

다음과 같은 16,
+0

데이터 크기 = "400", "px"를 추가하는 것을 잊지 마세요. – enguerranws

+0

@enguerranws ​​완료. 코드를 바이올린으로 업데이트했습니다! ': P' 작품! –

+0

실제로 더 이상 this.style.size가 필요하지 않습니다. 어쩌면 당신이'onload = "this.setAttribute ("data-size ", this.style.size);"를했을 때 그 부분이 좋을지도 모른다. ""pure JS를 사용하여 객체의 innerHeight를 얻는 것이 조금 더 어렵다. :) – somethinghere

1

당신은 일반 자바 스크립트에서

$(document).ready(function(){ 
     $('[data-size]').each(function(){ 
      $(this).height($(this).attr("data-size")); 
     }); 
}); 
관련 문제