简体   繁体   English

如何使用<a>标记中的</a> onclick()从另一个脚本更改变量

[英]How can I change a variable from another script with onclick() in an <a> tag

I have a script called main.js that is attached to index.html. 我有一个名为main.js的脚本,该脚本附加到index.html。 I have an onclick() function set up within index.html that needs to change the value of a variable. 我在index.html中设置了一个onclick()函数,该函数需要更改变量的值。 This question would be easy if the script was written with script tags, however, despite trying to figure it out, I'm not sure how to do it when the script is referenced via a script tag. 如果脚本是用脚本标签编写的,那么这个问题将很容易,但是,尽管试图弄清楚,但我不确定当通过脚本标签引用脚本时该怎么做。 The code isn't really relevant to the question, but here's the line anyway. 该代码与该问题并非真正相关,但是无论如何这行都是。

<li><a href="main.html" onclick='card=thing;'>thing</a></li>

card is a variable in main.js which needs to be changed. card是main.js中的变量,需要更改。 In main.js, before the onclick() event, card is set to NULL. 在main.js中,在onclick()事件之前,card设置为NULL。 Thanks so much. 非常感谢。

Write a function in the home.js and inside the function you can access and change the href variable. 在home.js中编写一个函数,然后在该函数内访问和更改href变量。

html should be like this html应该是这样的

<li><a  id="variable" href="main.html" onclick='card=thing;'>thing</a></li>

in thee function add this line 在你的功能添加这一行

document.getElementById("variable").href="your reference";

You can write function in script tags or in main.js, and call it onclick 您可以在脚本标签或main.js中编写函数,然后在onclick上调用它

<li><a href="javascript:void(0)" onclick='changeCard("thing")'>thing</a></li>
<script type="text/javascript">
    function changeCard(val) {
        card = val;
    }
</script>

I think your code is fine in terms of syntax, because if the main.js file is linked to the same html where this line is, then card is a global variable that you could override without any problem. 我认为您的代码在语法方面没问题,因为如果main.js文件链接到同一行所在的html,则card是一个全局变量,您可以毫无问题地覆盖它。

Though the problem in this case might be other, because you are changing that variable when user clicks an anchor tag which is pointing to a main.html file in its href attribute. 尽管这种情况下的问题可能是其他问题,因为当用户单击指向其href属性中指向main.html文件的锚标记时,您正在更改该变量。

So what will actually happen is that two event handlers will be executed, the first one will be your code changing the value of that variable and the second will actually redirect your window to another html file, changing the actual context of your current window and making its global variables no longer useful, and that makes no sense. 所以实际发生的是将执行两个事件处理程序,第一个将是您的代码来更改该变量的值,第二个将实际将您的窗口重定向到另一个html文件,从而更改当前窗口的实际上下文并它的全局变量不再有用,这没有任何意义。

If you want to do something in js before navigating somewhere else then you would need to attach an event handler to the anchor element and call event.preventDefault() method to prevent the default behavior of the anchor tag when it is clicked, in other words, prevent navigation. 如果要在导航到其他地方之前在js中做某事,则需要将事件处理程序附加到anchor元素,并调用event.preventDefault()方法,以防止单击anchor标记时的默认行为,换句话说,防止导航。 That would look something like this: 看起来像这样:

// Js code:
var anchor = document.querySelector('a')
anchor.addEventListener('click', function(event) {
    event.preventDefault()
    // Do the stuff you want to do on click here, before navigating, etc
    card = thing

    // ... explicitly navigate
})

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

相关问题 如何访问在另一个变量上导入的变量<script> tag in JS - How can I access a variable that is imported on another <script> tag in JS 如何点击HTML变量更改? - How can I onclick an HTML variable change? 如何从另一个脚本调用变量以执行功能 - How can I call a variable from another script to execute a function 如何从另一个脚本访问 VBScript 之外的变量? - How can I access a Variable outside of a VBScript from another Script? 如何在应用脚本中将一个 script.gs 中的变量调用到另一个 script.gs - How can i call a variable from one script.gs to another script.gs in app script 如何将 output 更改为此脚本的变量 - How can I change the output to a variable of this script 如何使用onclick函数将div标签元素与另一个div标签元素调用 - How can I call div tag element with another div tag element using onclick function 如何从另一个页面 onclick 更改 iframe 的“src”? - How do I change 'src' of an iframe from another page onclick? 我如何将脚本运行到从jquery加载特定标签加载的另一个页面 - how i can run a script to another page loaded from jquery load specific tag 如何在 ejs 文件内的脚本标记中使用此变量 - How can I use this variable in script tag inside ejs file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM