开发者

Basic jQuery problems

I am struggling with jQuery for a long time now. It is very powerful and there are lot of great things we can do with jQuery.

My problem is that I use a lot of jQuery features at the same time. E.g. I have a site that displays items, 12 items per page and I can paginate through the pages using jQuery. On the same page I implemented a thumpsUp button that uses jQuery too.

The more 开发者_如何学GojQuery features I use, the harder it gets to arrange them properly. E.g.:

$(document).ready(function() {
 $(".cornerize").corner("5px"); //cornerize links
 $('a#verd').live('click', exSite); //open iframe
 $("a.tp").live('click', thumpsUp); //thumps up
 $("a#next").click(getProgramms); //next page
 $("a#previous").click(getProgramms); //previous page
    //for the current page reload the content
 $("a#page").each(function() {
  $(this).click(getProgramms);
 });
 //this isn't working...
 $('.smallerpost').live('click', alert('test'));
});

Have a look at the last code line. I want to perform an alert when the div element is clicked. Instead of doing so the page shows me the alert when I refresh the page. A click on the div has no effect.

What am I doing wrong? What would be a strategy here to have clean and working jQuery?


Change that line to

$('.smallerpost').live('click', function () {
    alert('test');
});

and while you're there...

$("a#page").each(function() {
    $(this).click(getProgramms);
});

has exactly the same effect as:

$('a#page').click(getProgramms);

... but technically there should be only one element with id='page' anyway


Your code $('.smallerpost').live('click', alert('test')); calls the alert immediately and passes its return value into the live function as the second parameter. What you want to pass there is a function to call, so you want:

$('.smallerpost').live('click', function() {
    alert('test');
});

or

$('.smallerpost').live('click', handleSmallerPostClick);

function handleSmallerPostClick() {
    alert('test');
}

...depending on how you structure your code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜