Use onclick in a tag to prompt that the function is not defined

<a href="javascript:;" onclick="text()">leave a message</a>

When I was working on something today, I found that when I used the onclick trigger function in the a tag, I kept reporting an error, showing that the function was undefined. At first I thought it was because I hadn't written the code for too long and couldn't write it.
Repeated operations and Baidu later. It was found that it was a function naming problem. One argument I found is that the
function name cannot be the same as the id name of a tag on the page. Some browsers can access the node element by specifying the ID in the js code, and then the defined function will be overwritten by the element in the DOM. You need to rename the function name or element ID. This statement cannot solve my problem. I guess from this that text keywords are used in the native code , so these keywords should be avoided when defining functions.
Let me talk about the solution.
1. Change the name of the function, change it all directly, or add an underscore before the original name. Example: _text.
2. <a href="javascript:text();">leave a message</a>This method is not very rigorous. Its principle is in JavaScript: js code will be executed later. But there is a question: Why is there no conflict in this name, or my previous statement is wrong. I think from the root of the a tag, look
<a>at the href attribute of the tag, where href is an acronym for hypertext reference, used to set the link address. The link address must be a url address. If no specific path is given, the default path is the same as the path of the current page.
Is there a saying that the operating methods of these two code forms are different.
<a href="javascript:text();">leave a message</a>Directly point to the method in the introduced js code block. Thus avoiding encounters with unknown conflicts. But this method is not recommended.
It not only exposes the method you use, but also is not rigorous.javascript: 是一个伪协议,其他的伪协议还有 mail: tel: file: 等等。javascript:是表示在触发默认动作时,执行一段JavaScript代码

Insert picture description here
3. Another method is
window.editServer = function(value){

}, my use is not effective, see many articles are used this method, then hang it up

Just write this first, this article refers to: https://blog.csdn.net/chenchunlin526/article/details/77346049
There is no official statement about the text conflict above, why the second method does not conflict. If you see this article, thank you for answering, welcome to discuss~

Guess you like

Origin blog.csdn.net/weixin_45834446/article/details/107172055