当我的页面加载时,我无法将元素的值传递给jquery函数

时间:2011-05-23 22:55:14

标签: javascript-events jquery-selectors jquery

$('body').load(
  // Handler for .load() called.
  amazingfunction("#chairleg");
);

function amazingfunction(str){
    // do ajax stuff with the variable
    // this bit works fine. i just want to auto load the element into this.
}

有人可以帮忙吗?我试图在页面加载时立即获取#chairleg的html内容,并在传递变量时直接调用函数'amazingfunction'。

我似乎无处可去。我也用以下代替第3行尝试了它,但仍然没有运气:

$("#chairleg").html;
奇怪的是,当我有一个半冒号时,我似乎也会遇到错误; '在第3行的结尾。关于这两个问题的任何想法?我猜他们是联系在一起的,问题2将在问题1解决后解决吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

$(function () {
   amazingfunction($("#chairleg").html());
});

function amazingfunction(htm){
    //you have the html of chairleg in htm variable
    // do ajax stuff with the variable
    // this bit works fine. i just want to auto load the element into this.
}

答案 1 :(得分:0)

您需要提供回调函数。你实际上并没有创建函数......

此外,body没有load事件(尽管它确实有onload属性)。使用document及其ready事件。

$(document).ready(function()
    amazingfunction("#chairleg");
);

要获取元素的HTML内容,请使用$('selector').html()。所以在这里你可以使用

amazingfunction($('#chairleg').html());