Possible? When a function in a javascript file is

2019-08-29 06:35发布

Could I create a function or custom event in a javascript file called "justDidStuff" and then make .live() watch for that being triggered in another jQuery file?

I know this sounds really complicated, but I can't think of another way to do this.

I have new content coming in from the javascript file which is the only infinite scroller known to work for Tumblr.

I have a bunch of styling happening on the layout of the incoming posts (http://syndex.me) which i'm obviously going to make with jQuery. Hence i'm in a situation where I

A) have to use .live() (posts are dynamically loaded) and B) can't trigger the changes in a straightforward manner

In a previous question related to this DOMNodeInserted was reluctantly suggested. This just listens for when something has been changed, but it slows down pages such as this and has been depreciated.


EDIT

http://marckremers.com/syndex/js/jquery.infinitescrollfortumblr.js Is the javascript file (NB it's a monster)

http://marckremers.com/syndex/js/jquery.syndex.js Is my Styling and Site behaviour jQuery file.

3条回答
我想做一个坏孩纸
2楼-- · 2019-08-29 07:11

You can use bind and trigger.

var justDidStuff = function(){
    //do some stuff
}

$('something').bind('justDidStuff',justDidStuff); // binds to all elements, 
                                                  // now and in the future.

//call it:
$('something').trigger('justDidStuff');
查看更多
Melony?
3楼-- · 2019-08-29 07:24

I think you want to use trigger() and bind

Something like this:

jQuery("body").bind("myEvent", function( data ){ alert("triggered"); } );

and in your function you can notify the page

jQuery("body").trigger("myEvent", { "foo", "bar" });
查看更多
小情绪 Triste *
4楼-- · 2019-08-29 07:35

OK I have solved the problem. It's so easy i can't believe it. You live and learn. All I had to do was place jquery code inline within the javascript file by doing this:

jQuery(function ($) {//doStuff in a javascript file as normal, OMG}
查看更多
登录 后发表回答