jquery地址和live方法
//deep linking
$.fn.ajaxAnim = function() {
$(this).animW();
$(this).html('<div class="load-prog">loading...</div>');
}
$("document").ready(function(){
contM = $('#main-content');
contS = $('#second-content');
$(contM).hide();
$(contM).addClass('hidden');
$(contS).hide();
$(contS).addClass('hidden');
function loadURL(URL) {
//console.log("loadURL: " + URL);
$.ajax({ url: URL,
beforeSend: function(){$(contM).ajaxAnim();},
type: "POST",
dataType: 'html',
data: {post_loader: 1},
success: function(data){
$(contM).html(data);
$('.post-content').initializeScroll();
}
});
}
// Event handlers
$.address.init(function(event) {
//console.log("init: " + $('[rel=address:' + event.value + ']').attr('href'));
}).change(function(event) {
evVal = event.value;
if(evVal == '/'){return false;}
else{
$.ajax({ url: $('[rel=address:' + evVal + ']').attr('href'),
beforeSend: function(){$(contM).ajaxAnim();},
type: "POST",
dataType: 'html',
data: {post_loader: 1},
success: function(data){
$(contM).html(data);
$('.post-content').initializeScroll();
}});
}
//console.log("change");
})
$('.update-main a, a.update-main').live('click', function(){
loadURL($(this).attr('href'));
return false;
});
$(".update-second a, a.update-second").live('click', function() {
var link = $(this);
$.ajax({ url: link.attr("href"),
beforeSend: function(){$(contS).ajaxAnim();},
type: "POST",
dataType: 'html',
data: {post_loader: 1},
success: function(data){
$(contS).html(data);
$('.post-content').initializeScroll();
}});
return false;
});
});
我使用 jquery 地址来更新内容,同时维护有用的 url。当点击主导航中的链接时,url 会正确更新,但是当使用 ajax 动态加载链接时,url 地址功能会中断。
我已经使“点击”事件活动起来,允许通过动态加载的链接加载内容,但我似乎无法使地址事件侦听器活动起来,但这似乎是实现此目的的唯一方法。 我的语法是否错误?
$.address.change(function(event) {
如果我将 this : 更改为 this: ,
$.address.live('change', function(event) {
或者 live 方法不适用于此插件?
//deep linking
$.fn.ajaxAnim = function() {
$(this).animW();
$(this).html('<div class="load-prog">loading...</div>');
}
$("document").ready(function(){
contM = $('#main-content');
contS = $('#second-content');
$(contM).hide();
$(contM).addClass('hidden');
$(contS).hide();
$(contS).addClass('hidden');
function loadURL(URL) {
//console.log("loadURL: " + URL);
$.ajax({ url: URL,
beforeSend: function(){$(contM).ajaxAnim();},
type: "POST",
dataType: 'html',
data: {post_loader: 1},
success: function(data){
$(contM).html(data);
$('.post-content').initializeScroll();
}
});
}
// Event handlers
$.address.init(function(event) {
//console.log("init: " + $('[rel=address:' + event.value + ']').attr('href'));
}).change(function(event) {
evVal = event.value;
if(evVal == '/'){return false;}
else{
$.ajax({ url: $('[rel=address:' + evVal + ']').attr('href'),
beforeSend: function(){$(contM).ajaxAnim();},
type: "POST",
dataType: 'html',
data: {post_loader: 1},
success: function(data){
$(contM).html(data);
$('.post-content').initializeScroll();
}});
}
//console.log("change");
})
$('.update-main a, a.update-main').live('click', function(){
loadURL($(this).attr('href'));
return false;
});
$(".update-second a, a.update-second").live('click', function() {
var link = $(this);
$.ajax({ url: link.attr("href"),
beforeSend: function(){$(contS).ajaxAnim();},
type: "POST",
dataType: 'html',
data: {post_loader: 1},
success: function(data){
$(contS).html(data);
$('.post-content').initializeScroll();
}});
return false;
});
});
I'm using jquery addresses to update content while maintaining a useful url. When clicking on links in a main nav, the url is updated properly, but when links are loaded dynamically with ajax, the url address function breaks.
I have made 'click' events live, allowing for content to be loaded via dynamically loaded links, but I can't seem to make the address event listener live, but this seems to be the only way to make this work. Is my syntax wrong if I change this :
$.address.change(function(event) {
to this:
$.address.live('change', function(event) {
or does the live method not work with this plugin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
进入jquery地址插件文件并将这一行:替换
为:
使所有动态加载的链接响应地址插件事件侦听器
went into the jquery address plugin file and replaced this line:
with this :
to make all dynamically loaded links respond to the address plugin event listener
live 方法仅适用于 jquery dom 元素。不带插件。
live 方法检测 dom 元素是否动态添加到 HTML 并将事件绑定到它。你想说的是:“如果“地址”插件出现在html上,请将事件“更改”绑定到它”。 “地址”插件不是 dom 元素,它不起作用。
The live method only works jquery dom elements. Not with plugins.
The live method detects if a dom element was dynamically added to the HTML and binds an event to it. What you're trying to say there is: "If the "address" plugin appears on the html, bind the event "change" to it". Has the "address" plugin isn't a dom element, it doesn't work.
首先,简短的回答:不,它不起作用:)
原因部分:
.live()
作用于事件,它不关心它们是否是自定义等(在大多数情况下)。最热门的是当您像这样调用.live()
时,例如:.live()
在 DOM 的根部创建一个事件处理程序,等待事件冒泡到它。重要的部分:新元素或旧元素以相同的方式冒泡事件,无论它们如何加载。当事件冒泡并到达 DOM 根时,.live()
事件处理程序会检查其选择器是否与具有该事件的元素匹配,如果是,则执行。使用插件,您想说“嘿,新东西”并在其上运行代码...正如您所看到的,这与
.live()
内部的实际工作方式非常不同。First, the short answer: No, it doesn't work :)
The why part:
.live()
works off events, it doesn't care if they're custom, etc (in most cases). Hot it works is when you call.live()
like this for example:.live()
creates an event handler at the root of the DOM waiting for events to bubble up to it. The important part: new or old elements bubble events the same way, doesn't matter how they were loaded. When the event bubbles up and reaches the DOM root, the.live()
event handlers check if their selector matches the element that had the event, and if so they execute.With a plugin you want to say hey new something and run code on it...as you can see this is very different from how
.live()
actually works on the inside.