gpt4 book ai didi

javascript - 按类将函数应用于 div

转载 作者:行者123 更新时间:2023-11-30 07:59:26 25 4
gpt4 key购买 nike

我认为我这样做是对的,但它不起作用。我觉得我很愚蠢...

function expand(){

var wide = $(this).css('width');
var high = $(this).css('height');

var newwide = wide * 10;
var newhigh = high * 10;

$(this).animate({'width':newwide,'height':newhigh}, 2000);

}

$('.object1').expand();

我只想在类为“object1”的 div 上运行这个“扩展”函数,我做错了什么?

谢谢

最佳答案

在 jQuery 中,要添加自定义函数,您可以这样做:

(function($){

$.fn.myFunction = function() {
//Custom Function
}

})(jQuery);

因此,您的代码应如下所示:

(function($){
$.fn.expand = function() {

var wide = $(this).css('width');
var high = $(this).css('height');

var newwide = wide * 10;
var newhigh = high * 10;

$(this).animate({'width':newwide,'height':newhigh}, 2000);

}
})(jQuery);

Learn More

关于javascript - 按类将函数应用于 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com