在 Ajax 之后重新绑定菜单 JS
Rebind Menu Js after Ajax
我正在构建一个在线URL拍卖工具。下面是用户输入要购买的 URL 后的结果页面。当此结果页面被称为时,正在创建其他 2 个结果页面,但它们需要一段时间,所以我要做的是设置菜单栏以在两个按钮上读取"加载"并将 href 设置为 #。然后,我使用 setInterval 每 2 秒轮询一次,检查是否已创建第二个文件。 一旦完成,成功回调会将按钮重新加载到活动的hrefs并标记它们["找出谁"和"评估"]而不是"加载"。
问:如何重新绑定菜单栏 js 以使其在 ajax 调用后工作?我正在阅读有关此确切主题的SO,但在使用.live(),.on(),display()等时失败了...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Domain Auctions R US</title>
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen"/>
<style>
body{
background-repeat:no-repeat;
background-color:#fff;
}
span.reference{
position:fixed;
left:10px;
bottom:10px;
font-size:12px;
}
span.reference a{
color:#aaa;
text-transform:uppercase;
text-decoration:none;
text-shadow:1px 1px 1px #000;
margin-right:30px;
}
span.reference a:hover{
color:#ddd;
}
ul.sdt_menu{
margin-left:auto;
margin-right:auto;
margin-top:0px;
}
</style>
</head>
<body>
<br/><br/><br/><br/>
<div class="content" id="content">
<ul id="sdt_menu" class="sdt_menu">
<li>
<a href="#"><!-- When the other two results pages are ready, this needs to be a real link -->
<img src="../images/googleMap.jpg" alt=""/>
<span class="sdt_active"></span>
<span class="sdt_wrap">
<span class="sdt_link">Loading</span>
<span class="sdt_descr">Page not yet available</span>
</span>
</a>
</li>
<li>
<a href="worth.html">
<img src="../images/pawnShop.jpg" alt=""/>
<span class="sdt_active"></span>
<span class="sdt_wrap">
<span class="sdt_link">Loading</span>
<span class="sdt_descr">Page not yet available</span>
</span>
</a>
</li>
</ul>
</div>
<h1>The URL you are searching for is currenly TAKEN.</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/jquery.easing.1.3.js"></script>
<script type="text/javascript">
$(function() {
$('#sdt_menu > li').bind('mouseenter',function(){ //LINE SWITCH 1
var $elem = $(this);
$elem.find('img')
.stop(true)
.animate({
'width':'170px',
'height':'170px',
'left':'0px'
},400,'easeOutBack')
.andSelf()
.find('.sdt_wrap')
.stop(true)
.animate({'top':'140px'},500,'easeOutBack')
.andSelf()
.find('.sdt_active')
.stop(true)
.animate({'height':'170px'},300,function(){
var $sub_menu = $elem.find('.sdt_box');
if($sub_menu.length){
var left = '170px';
if($elem.parent().children().length == $elem.index()+1)
left = '-170px';
$sub_menu.show().animate({'left':left},200);
}
});
}).bind('mouseleave',function(){ //LINE SWITCH 2
var $elem = $(this);
var $sub_menu = $elem.find('.sdt_box');
if($sub_menu.length)
$sub_menu.hide().css('left','0px');
$elem.find('.sdt_active')
.stop(true)
.animate({'height':'0px'},300)
.andSelf().find('img')
.stop(true)
.animate({
'width':'0px',
'height':'0px',
'left':'85px'},400)
.andSelf()
.find('.sdt_wrap')
.stop(true)
.animate({'top':'25px'},500);
});
});
var myVar=setInterval(function(){ajax_request()},2000);
function ajax_request() {
$.ajax({
type: "GET",
url: "http://exampleServer/urlID/4b49d2/index.html",
dataType: "script",
success: function() {
$('#conn').load("ajax-loader.html");
clearInterval(myVar);
},
error: function() {
alert("error");
}
});
}
</script>
</body>
</html>
ajax-loader.html
<ul id="sdt_menu" class="sdt_menu">
<li>
<a href="../map.html">
<img src="../images/googleMap.jpg" alt=""/>
<span class="sdt_active"></span>
<span class="sdt_wrap">
<span class="sdt_link">Find out Whois</span>
<span class="sdt_descr">Map of Who Owns Domain</span>
</span>
</a>
</li>
<li>
<a href="worth.html">
<img src="../images/pawnShop.jpg" alt=""/>
<span class="sdt_active"></span>
<span class="sdt_wrap">
<span class="sdt_link">Appraisal</span>
<span class="sdt_descr">Real Time Value of Domain</span>
</span>
</a>
</li>
</ul>
这应该很简单,使用 .on()
而不是 .bind()
:
$('#content')
.on('mouseenter', '.sdt_menu > li', function() {
})
.on('mouseleave', '.sdt_menu > li', function() {
})
请注意,.bind()
和.on()
的工作方式不同,简单的重命名不起作用。
可以使用委托
将事件绑定到所有当前和将来的元素:
http://api.jquery.com/delegate/
相关文章:
- 重置为下拉菜单中的未绑定选项
- 具有任意绑定的角度多选菜单
- 通过单向绑定的过滤功能提高自定义角度下拉菜单中的角度性能
- 将函数绑定到用作子菜单的无序列表
- jQuery Mobile 选择菜单数据绑定
- Jquery插件菜单 - 绑定或实时
- 如何将菜单项绑定到同一解决方案中的另一个项目
- 在菜单项上绑定值,点击Angular2材质设计
- 将三个选择菜单绑定在一起
- 将作用域与侧菜单绑定是行不通的
- 在angular.js的select下拉菜单中绑定值和文本
- 在AngularJs中绑定Key-Value对Object时,在下拉菜单中设置默认选择
- jQuery.在两个相互叠加的元素上绑定上下文菜单事件
- 如何将单选按钮绑定到下拉菜单中
- 如何使用knockoutjs点击绑定来创建汉堡包菜单
- 绑定复杂的json对象到下拉菜单
- Knockout -如何订阅多个下拉菜单(选择)并绑定到模型
- jQuery -上下文菜单-解除绑定
- 当尝试绑定ajax和控制器动作时,下拉菜单没有填充
- 在 Ajax 之后重新绑定菜单 JS