我们来详细看一下。我们平常为元素绑定事件是这样做的,以我做的实验为例:
在主页面如main.php中加入
- <script type="text/javascript"
- src="<?php
- echo base_url ( "items/js/index/base_all.js" )?>"></script>
- <div class="product_photo"><a href=""><img
- src=<?php
- echo base_url ( $picture_path );
- ?> alt=""
- class="product_focus"></img></a></div>
- $(function() {
- $(".product_focus").bind(
- 'mouseover',
- function() {
- $(this).parent().parent().parent().parent().parent().children(
- '.product_display').css(
- {
- top : $(this).position().top + "px",
- left : $(this).position().left - 15
- + $(this).outerWidth(false) + "px"
- });
- $(this).parent().parent().parent().parent().parent().children(
- '.product_display').show();
- });
- $(".product_focus").bind('mouseleave', function() {
- $(".product_display").hide();
- });
- });
但是如果main.php中的这段代码是Ajax服务器返回的,Jquery特效就不会起一点作用。
如:
- $.ajax({
- type:"POST",
- url:"<?php echo site_url("ajax_part/getProductDetail");?>",
- success:function(data)
- {$(".just").empty();
- $(".just").html(data);
- }
- });
不用包含base_all.js这个JS文件,而把其中的Jquery控制代码写入$.ajax中。如下:
- $.ajax({
- type:"POST",
- url:"<?php echo site_url("ajax_part/getProductDetail");?>",
- success:function(data)
- {
- $(".just").empty();
- $(".just").html(data);
- afterLoad();
- }
- });
- function afterLoad()
- {
- $(function() {
- $(".product_focus").bind(
- 'mouseover',
- function() {
- $(this).parent().parent().parent().parent().parent().children(
- '.product_display').css(
- {
- top : $(this).position().top + "px",
- left : $(this).position().left - 15
- + $(this).outerWidth(false) + "px"
- });
- $(this).parent().parent().parent().parent().parent().children(
- '.product_display').show();
- });
- $(".product_focus").bind('mouseleave', function() {
- $(".product_display").hide();
- });
- }
