最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I add more than one function in my $(document).ready function - Stack Overflow

programmeradmin0浏览0评论

I have this and it works fine:

$(document).ready(
    highlightTableRow
);

but when I add a second function (see below) the second doesn't work.

$(document).ready(
    highlightTableRow,
    attachClickLinkHandlerForRowLink
);

What's the correnct syntax for adding a second function to my ready function? Thanks.

edit: add syntax errors. (eclipse)

$(document).ready(
    highlightTableRow();  **// error:Syntax error, insert ")" to plete Arguments**
    attachClickHandlerForRowLink();  **//error: Missing semicolon**
); **// error: Syntax error on token ")", delete this token**


var originalRowBackground;

function highlightTableRow(){
    $('[class^="contentRow"]:has(a)').live('mouseenter', enterRowFunction).live('mouseleave', exitRowFunction);
}

function enterRowFunction(){
    originalRowBackground = $(this).css('background-color');
    $(this).css({'background-color': "#EFE3FF", 'cursor': 'pointer'});
}

function exitRowFunction(){
    $(this).css({'background-color': originalRowBackground, 'cursor': 'pointer'});
}

function attachClickHandlerForRowLink(){
    $('[class^="contentRow"]:has(a)').live('click', clickRowLink);
}

function clickRowLink(){
    window.location = $(this).find("a").attr("href");
}    **//error: Missing semicolon**

I have this and it works fine:

$(document).ready(
    highlightTableRow
);

but when I add a second function (see below) the second doesn't work.

$(document).ready(
    highlightTableRow,
    attachClickLinkHandlerForRowLink
);

What's the correnct syntax for adding a second function to my ready function? Thanks.

edit: add syntax errors. (eclipse)

$(document).ready(
    highlightTableRow();  **// error:Syntax error, insert ")" to plete Arguments**
    attachClickHandlerForRowLink();  **//error: Missing semicolon**
); **// error: Syntax error on token ")", delete this token**


var originalRowBackground;

function highlightTableRow(){
    $('[class^="contentRow"]:has(a)').live('mouseenter', enterRowFunction).live('mouseleave', exitRowFunction);
}

function enterRowFunction(){
    originalRowBackground = $(this).css('background-color');
    $(this).css({'background-color': "#EFE3FF", 'cursor': 'pointer'});
}

function exitRowFunction(){
    $(this).css({'background-color': originalRowBackground, 'cursor': 'pointer'});
}

function attachClickHandlerForRowLink(){
    $('[class^="contentRow"]:has(a)').live('click', clickRowLink);
}

function clickRowLink(){
    window.location = $(this).find("a").attr("href");
}    **//error: Missing semicolon**
Share Improve this question edited Dec 2, 2011 at 17:33 Dale asked Dec 2, 2011 at 17:10 DaleDale 1,3013 gold badges16 silver badges37 bronze badges 5
  • 3 add parens () and semicolons ; after your function calls – Evan Commented Dec 2, 2011 at 17:12
  • 1 All answers are same.. and they should be.. – talha2k Commented Dec 2, 2011 at 17:14
  • 1 @AlphaMale you wouldn't believe it ... some answers was errata maximus ;) (now fixed... I hope...) 8D – Roko C. Buljan Commented Dec 2, 2011 at 17:16
  • @Evan That was the first thing I tried, but it doesn't work. I'll update the original post for clarity to add error messages. – Dale Commented Dec 2, 2011 at 17:30
  • Thanks all. Got it. I missed the part about wrapping it in a function. – Dale Commented Dec 2, 2011 at 17:37
Add a ment  | 

5 Answers 5

Reset to default 9

you could do

$(document).ready(function() {
    highlightTableRow();
    attachClickLinkHandlerForRowLink();
});

also you could change the $(document).ready() part into $(function() so you would get

$(function() {
    highlightTableRow();
    attachClickLinkHandlerForRowLink();
});

does the same only shorter

try like:

$(document).ready(function(){
    highlightTableRow();
    attachClickLinkHandlerForRowLink();
});

I would do something like this:

$(document).ready(function()
{
    highlightTableRow();
    attachClickLinkHandlerForRowLink();

});

Am I missing the point?

Why don't you use like this?

$(document).ready(function(){
    highlightTableRow();
    attachClickLinkHandlerForRowLink();
});

document.ready takes a function

$(document).ready(function() {
    highlightTableRow();
    attachClickLinkHandlerForRowLink();
});
发布评论

评论列表(0)

  1. 暂无评论