>  기사  >  웹 프론트엔드  >  놓치지 말아야 할 일곱 가지 jQuery 팁_jquery

놓치지 말아야 할 일곱 가지 jQuery 팁_jquery

WBOY
WBOY원래의
2016-05-16 15:24:211787검색

jQuery는 경량 JavaScript 라이브러리이자 가장 인기 있는 클라이언트 측 HTML 스크립트 중 하나이며 웹 디자이너와 개발자 사이에서 매우 유명하며 유용한 플러그인과 기술이 많이 있습니다.

이 기사에서는 몇 가지 jQuery 팁을 공유합니다.

1. 새 창에서 링크를 엽니다

다음 코드를 사용하면 링크를 클릭하여 새 창에서 열 수 있습니다.

$(document).ready(function() {
  //select all anchor tags that have http in the href
  //and apply the target=_blank
  $("a[href^='http']").attr('target','_blank');
});

2. 열의 높이를 동일하게 설정하세요

웹 애플리케이션의 각 열 높이를 대기시키려면 다음 코드를 적용하세요.

<div class="equalHeight" style="border:1px solid">
  <p>First Line</p>
  <p>Second Line</p>
  <p>Third Line</p>
</div>
<div class="equalHeight" style="border:1px solid">
  <p>Column Two</p>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  equalHeight('.equalHeight');
});
//global variable, this will store the highest height value
var maxHeight = 0;

function equalHeight(col) {
  //Get all the element with class = col
  col = $(col);
  //Loop all the col
  col.each(function() {
    //Store the highest value
    if ($(this).height() > maxHeight) {
      maxHeight = $(this).height();
    }
  });
  //Set the height
  col.height(maxHeight);
}
</script>

3. jQuery 사전 로드 이미지

이 작은 트릭을 사용하면 페이지의 이미지 로드 속도를 향상시킬 수 있습니다.

jQuery.preloadImagesInWebPage = function() {
  for (var ctr = 0; ctr & lt; arguments.length; ctr++) {
    jQuery("").attr("src", arguments[ctr]);
  }
}

// 使用方法:
$.preloadImages("image1.gif", "image2.gif", "image3.gif");

// 检查图片是否被加载
$('#imageObject').attr('src', 'image1.gif').load(function() {
  alert('The image has been loaded…');
});

4. 마우스 오른쪽 버튼 비활성화

$(document).ready(function() {
  //catch the right-click context menu
  $(document).bind("contextmenu", function(e) {
    //warning prompt - optional
    alert("No right-clicking!");

    //delete the default context menu
    return false;
  });
});

5. 타이머 설정

$(document).ready(function() {
  window.setTimeout(function() {
    // some code
  }, 500);
});

6. 하위 요소 수 계산

<div id="foo">
  <div id="bar"></div>
  <div id="baz">
    <div id="biz"></div>
    <span><span></span></span>
  </div>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  //jQuery code to count child elements $("#foo > div").size()
alert($("#foo > div").size())
</script>

7. 요소를 페이지 중앙에 배치하세요.

<div id="foo" style="width:200px;height: 200px;background: #ccc;"></div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery.fn.center = function() {
  this.css("position", "absolute");
  this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
  this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
  return this;
}
//Use the above function as:
$('#foo').center();
</script>

이 글을 읽은 친구들은 여러분이 배울 기회를 놓치지 않았다는 것을 알게 될 것입니다. 이러한 팁을 축적하면 모든 사람이 창의적이고 아름다운 웹 페이지를 디자인하는 데 확실히 도움이 될 것입니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.