简体   繁体   English

OnClick按钮在另一页上不起作用

[英]OnClick Button Doesn't Work at Another Page

I got two buttons and they both work perfectly. 我有两个按钮,它们都工作正常。 Then, I needed both in some other page of my site and I simply copied and pasted the codes over there, buttons showed up but they don't work. 然后,我需要在网站的其他页面上都需要,我只是简单地将代码复制并粘贴到那里,出现了按钮,但它们不起作用。

<input style="outline: none;" type="button" onclick="formatTextlink ('http://');" class="btn btn-default" value="link">

<input style="outline: none;" type="button" onclick="formatText ('bkz:');" class="btn btn-default" value="bkz:">

I get this error: 我收到此错误:

Uncaught ReferenceError: formatTextlink is not defined at HTMLInputElement.onclick 未捕获的ReferenceError:在HTMLInputElement.onclick上未定义formatTextlink

Don't know really what to do. 真的不知道该怎么办。 Any help is appreciated. 任何帮助表示赞赏。 Thank you already! 已经谢谢你了!

未捕获的ReferenceError:未定义formatTextlink当您调用的函数在页面中不可用或js脚本中的错误时,发生此错误,请检查该函数是否可用,否则在需要函数调用的页面中调用具有该功能的js。

Uncaught ReferenceError main simply that reference not found . 未被引用的ReferenceError主要只是找不到引用

Mains that formatTextlink is not defined in the current page. 指出在当前页面中未定义formatTextlink。 So you should reimport the .js where your functions are defined. 因此,应在定义函数的位置重新导入.js。

I suggest to put thos functions in a common file (for example, commonFunctions.js) and reimport this file when you need the two buttons. 我建议将这些函数放在一个通用文件(例如,commonFunctions.js)中,并在需要两个按钮时重新导入该文件。

commonFunctions.js and Your page (example) commonFunctions.js和您的页面(示例)

 function formatText(tag) { var Field = document.getElementById('entry_girdi'); var val = Field.value; var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd); var before_txt = val.substring(0, Field.selectionStart); var after_txt = val.substring(Field.selectionEnd, val.length); Field.value += '[' + tag + '/]'; } function formatTextlink(tag) { var Field = document.getElementById('entry_girdi'); var val = Field.value; var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd); var before_txt = val.substring(0, Field.selectionStart); var after_txt = val.substring(Field.selectionEnd, val.length); Field.value += '' + tag + ''; } 
 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="commonFunctions.js"></script> </head> <body> <input style="outline: none;" type="button" onclick="formatTextlink ('http://');" class="btn btn-default" value="link"> <input style="outline: none;" type="button" onclick="formatText ('bkz:');" class="btn btn-default" value="bkz:"> </body> </html> 

I sure forgot to include js codes in my second page. 我确定忘了在第二页中包含js代码。 I had to add these following lines to get my buttons work. 我必须添加以下几行以使按钮正常工作。 I thought that putting the js code once was enough to make all the buttons work, that was my mistake. 我以为,一次放置js代码足以使所有按钮正常工作,这是我的错误。 So here's the solution, sorry and thanks everyone! 所以这是解决方案,对不起,谢谢大家!

 <script type="text/javascript"> function formatText(tag) { var Field = document.getElementById('entry_girdi'); var val = Field.value; var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd); var before_txt = val.substring(0, Field.selectionStart); var after_txt = val.substring(Field.selectionEnd, val.length); Field.value += '[' + tag + '/]'; } </script> <script type="text/javascript"> function formatTextlink(tag) { var Field = document.getElementById('entry_girdi'); var val = Field.value; var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd); var before_txt = val.substring(0, Field.selectionStart); var after_txt = val.substring(Field.selectionEnd, val.length); Field.value += '' + tag + ''; } </script> 

确保已使用<script>标记或meta标记在此页面中导入了JavaScript源代码(定义了函数)。

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

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