开发者

how do you use a custom function with jquery .live()?

开发者 https://www.devze.com 2023-01-28 11:04 出处:网络
I have a page that has haschhange tabs so when I click on a link the url looks like www.site.com/abc.php#123.php

I have a page that has haschhange tabs so when I click on a link the url looks like www.site.com/abc.php#123.php

but because of this my functions don't work.

I have a page specific script that works if i go to www.site.com/123.php. But it won't work when the page is loaded in via ajax by the hashchange plugin.

I was thinking that I needed to use the .live() so that when the page loads it would capture the new content... hmmmm

is there another way of doing this?

I'm trying to run this code on my 123.php page:

$(".tab_content").hide(); //Hide all content $("#files_left_pane > ul.tabs li:first").addClass("active").show(); //Activate first tab $("#files_right_pane > .tab_content:first").show(); //Show first tab content //On Click Event $("#files_left_pane > ul.tabs li").click(function() { $("#files_left_pane > ul.tabs li").removeClass("active"); //Remove any "active" clas开发者_Go百科s $(this).addClass("active"); //Add "active" class to selected tab $("#files_right_pane > .tab_content").hide(); //Hide all tab content

var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content $(activeTab).fadeIn(); //Fade in the active ID content return false; });

UPDATE:

I have tried to wrap my script in $(selector).live('click', function(){}); but what I'm noticing is that any JS i place in the 123.php won't be excecuted once loaded via hashchange

UPDATE: Nevermind, I had to run the function in the code that initiated the haschange and loaded in content via ajax.

Thanks prodigitalson


I would j define the code youve provided as a function and then use that function as art of your success callback for the ajax:

function updateTabs(){
  // the code you provided
}

// exmaple ajax call:

$.get(theUrl, function(){
  // other logic you need

  updateTabs();
});

This allows you to reuse your code after/bfore any number of operations. I dont think live is really the issue hear because you want to run this after youve appended new content to the DOM but that content isnt unique to a single specific element being added from what i can tell.

0

精彩评论

暂无评论...
验证码 换一张
取 消