[英]How can I call the class slibing function in javascript anonymous function
[英]How can I modify an anonymous javascript function with tampermonkey?
這是我要替換的代碼塊:
$(document).ready(function () {
$(".button-purple").click(function () {
interval = $(this).attr('id');
name = $(this.attr('name');
if(Number($(this).val()) === 0) {
if(name == 'static') {
do this
}
else {
do this
}
}
else {
do this
}
});
});
我找不到任何有關嘗試替換該功能的文檔,因為它未命名。 是否可以替換整個javascript文件+刪除加載它的行/插入我自己的腳本? 非常感謝我能提供的任何幫助。
我不知道tampermonkey
,但是您可以嘗試以下操作:
function chickHandler() {
interval = $(this).attr('id');
name = $(this.attr('name');
if (Number($(this).val()) === 0) {
if (name == 'static') {
do this
} else {
do this
}
} else {
do this
}
}
}
function onReadyHandler() {
$(".button-purple").click(chickHandler);
}
$(document).ready(onReadyHandler);
當您執行諸如.click(function(){...})
,此處的function
稱為回調。 您必須發送一個函數作為回調。 不必匿名。
如果您只想刪除click事件處理程序,則只需說
var $element = $(".button-purple");
$element.off('click');
如果要刪除所有事件處理程序,則首先必須找出所有事件處理程序都存在,然后以迭代方式刪除它們。
var element = $element[0]; //Make sure the element is a DOM object and not jQuery Object.
// Use this line if you're using jQuery 1.8+
var attachedEvents = $._data(element,'events');
// Use this line if you're using jQuery < 1.8
var attachedEvents = $(element).data('events'); //Here you can also replace $(element) with $element as declared above.
for(var event in attachedEvents){
$element.off(event);
}
更新:
刪除所有必需的現有處理程序后,只需添加自己的事件處理程序(使用.on()
API)即可。
只需定義您的功能即可。
function yourFunction(){ /* your code */};
$element.on('click', yourFunction);
更新2:
由於您只想刪除click事件處理程序,因此這是滿足您目的的最簡單的代碼。
$(".button-purple").off('click').on('click', yourFunction);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.