$('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解决后解决吗?
答案 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());