如何传递对'这'转换为函数
How to pass a reference to 'this' into a function?
如何通过"this"引用按钮并将其传递到函数中?
$(document).on("click", ".join-group-btn", function () {
var groupid=$(this).val();
var button=$(this);
joinGroup(groupid, button);
});
然后我将joinGroup函数保存在我的库中的一个单独的页面上:
function joinGroup (groupid, buttonIn) {
var button = $(buttonIn);
var l = Ladda.create(document.querySelector(buttonIn));
}
Id不起作用,因为按钮是动态生成的。
这抛出:
Dom exception 12: An invalid or illegal string was specified
谢谢你的帮助!非常感谢。
document.querySelector需要一个选择器字符串。但是你的函数需要一个DOM对象,对吧?你可以通过这样:
function joinGroup (groupid, buttonIn) {
var l = Ladda.create(buttonIn[0]);
}
您应该将querySelector
与类似CSS的语法选择器一起使用
您的buttonIn
是一个jQuery对象元素
document.querySelector(buttonIn) // you need to pass a String selector
// like "#someId",
// not an jQuery object
例如:
$(document).on("click", ".join-group-btn", function () {
joinGroup( this.value, $(this), this.id );
});
function joinGroup ( value, $this, id) {
// console.log( value ); // "Some Value"
// console.log( $this ); // [Object] the clicked jQuery Element
// console.log( id ); // "someID"
// Now if you want to use querySelector
var theButton = document.querySelector("#"+ id); // The missing #
// Or Directly operate over the `button` Element object argument
// $this.css({color:"red"}); // Cause it's a jQuery element
}
相关文章:
- 如何在将Node异步函数转换为promise时使用Q库
- 如何分解jQuery"toggle”;函数转换为separate“;显示“;以及“;隐藏“;功能
- 将函数转换为jQuery插件
- jquery递归函数转换为非递归函数
- jQuery函数转换为MooTools
- 将javascript函数转换为jquery
- 将Window Onload函数转换为非阻塞
- 如何将下面的递归函数转换为纯函数
- 将jquery函数转换为js
- 如何将这个递归函数转换为迭代函数
- 将递归函数转换为异步 CPS 实现 (javascript)
- 通过在JavaScript中将函数转换为对象可以实现什么
- 从JavaScript函数转换为jQuery
- JS:将单参数函数转换为可链接函数
- 如何将此函数转换为 Angular 指令
- 从控制台中的工作函数转换为工作脚本.(请参阅编辑)
- 将图像的 onError 函数转换为返回函数
- jQuery:将每个函数转换为 for 循环
- 如何将 JavaScript forEach 循环/函数转换为 CoffeeScript
- Javascript 将声明为函数字典的函数转换为原型