简体   繁体   English

尽管在脚本中定义了功能,但为什么在检查时却未定义函数跳过和onclick?

[英]Why is function skipped and onclick not defined when inspecting, although defined in the script?

I am making a userscript to add two <a> elements to a page and modify other three. 我正在制作一个用户脚本,将两个<a>元素添加到页面并修改其他三个。 From these three, I need to remove the href and some other data-* unneeded attributes. 从这三个中,我需要删除href和一些其他data-*不必要的属性。 Then, I'd like to add a title and define onclick (call a function). 然后,我想添加一个title并定义onclick (调用函数)。 After that, I need to add the two <a> elements. 之后,我需要添加两个<a>元素。

However, the function that should be called onclick is skipped and console is throwing sendFeedback is not defined when clicking the two new <a> s. 但是,单击两个新的<a>时, sendFeedback is not defined应调用onclick的函数,并且控制台抛出sendFeedback is not defined Calling the function inside the script doesn't throw that error. 在脚本内调用该函数不会引发该错误。 Also, while I have an el.onclick = "sendFeedback(arg)" , there's no onclick, after inspecting the element. 另外,虽然我有一个el.onclick = "sendFeedback(arg)" ,但是在检查元素之后没有onclick。 The new ones, though, have, because they were created inside the script. 不过,由于它们是在脚本内部创建的,因此具有新的属性。

Here's the part of the script that is incorrect: 这是脚本中不正确的部分:

const classes = ["success", "danger", "warning"];
const feedback_types = ["tp-", "fp-", "naa-"];
const descriptions = ["description 1", "description 2", "description 3"];

function sendFeedback(verdict) {

  // Below is for testing purposes, the function has another content
  console.info("Reached end of function; verdict is " + verdict);
};

for (var i = 0; i < 3; i++) {
  console.log("Start of for loop with i = " + i); // (Just for testing, too)
  var el = document.getElementsByClassName("feedback-button on-post text-" + classes[i]); // The two first classes are common, and there are three more classes: text-success, text-danger and text-warning
  $(el).removeAttr("href data-method data-remote data-post-id");
  $(el).attr("title", descriptions[i]);
  // $(el).attr("onclick", "sendFeedback(feedback_types[i])"); // onclick is not defined when inspecting
  el.onclick = "sendFeedback(feedback_types[i])"; // onclick is not defined when inspecting, too!
  if (i == 0) {
    $(el).before('<a title="some title here" class="feedback-button on-post text-success" onclick="sendFeedback(\'tpu-\')">✓😮</a>'); // add the first anchor element
  } else if (i == 1) {
    $(el).after('<a title="some title here" class="feedback-button on-post text-danger" onclick="sendFeedback(\'fpu-\')">✗😮</a>'); // add the second anchor element

  }
}

// Add "cursor: pointer" to all feedback buttons
$(".feedback-button").css("cursor", "pointer");

As mentioned, while onclick is defined, the three old <a> s have not onclick and in the two new ones, when clicking, there is an error 'sendFeedback' is not defined . 如前所述,在定义onclick ,三个旧<a>没有onclick而在两个新的<a>'sendFeedback' is not defined单击时, 'sendFeedback' is not defined错误'sendFeedback' is not defined

Adding sendFeedback(feedback_types[1]) at the end of the script however, makes console output: 但是,在脚本末尾添加sendFeedback(feedback_types[1])将使控制台输出:

Start of for loop with i = 0
Start of for loop with i = 1
Start of for loop with i = 2
Reached end of function; verdict is fp-

How to make onclick be defined in the <a> s and avoid {function} is not defined ? 如何在<a>定义onclick并避免{function} is not defined

Instead of 代替

el.onclick = "sendFeedback(feedback_types[i])"; 

I would recommend the jQuery way (as you're already using jQuery) 我建议使用jQuery方式(因为您已经在使用jQuery)

$(el).click(function () {
    sendFeedback(feedback_types[i])
})

This should fix your problem. 这应该可以解决您的问题。

See alsof https://api.jquery.com/click/ 另请参见https://api.jquery.com/click/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM