简体   繁体   English

切换onclick事件onclick

[英]Switch onclick Event onclick

I have a function written to open / close a div using a placeholder for a hyperlink. 我写了一个函数,使用超链接的占位符打开/关闭div。

<a class="button" onclick="show('click-drop')">Open</a>

The onclick event then opens the div. 然后, onclick事件将打开div。

I have this onclick to close it: 我有这个onclick将其关闭:

<a class="button" onclick="hide('click-drop')">Close</a>

What I would like to do is have a single placeholder for a hyperlink that switches between the two onclick events. 我想做的是为单个超链接使用一个占位符,该超链接可以在两个onclick事件之间切换。 So onclick=show would switch to onclick=hide and vice versa. 因此, onclick=show将切换为onclick=hide ,反之亦然。

I have looked everywhere and cannot find a straightforward solution for my situation. 我到处都是,找不到适合我情况的直接解决方案。

Revised Question 修订的问题

Here's specifically what I am working with. 这就是我正在使用的。

function show(target) {
    document.getElementById(target).style.height = '300px';
}
function hide(target) {
    document.getElementById(target).style.height = '0px';
}

<a class="button" onclick="show('click-drop')">Open</a>
<div id="click-drop" style="height:0px">
    <a class="button" onclick="hide('click-drop')">Close</a>
</div>

Click Open and it opens / expands inline height style. 单击“ Open ,它将打开/展开内联高度样式。 Click Close and it closes. 单击Close ,它关闭。

I need to have the onclick toggle, but with Open changing to Close and reverse. 我需要使用onclick开关,但是将“ Open更改为“ Close并反转。

It would be ideal to keep the placement of the links. 保持链接的位置是理想的。 So the Open link outside the link is hidden until the Close link within the div is clicked. 因此,隐藏链接外部的“ Open链接,直到单击div中的“ Close链接。

I appreciate any feedback. 我感谢任何反馈。

You could dynamically change the onclick event of the button, but it's already awkward enough that you're using HTML to describe functionality. 您可以动态更改按钮的onclick事件,但是它已经很笨拙,无法使用HTML来描述功能。 It would do you even better if you kept all functionality in the JavaScript. 如果将所有功能都保留在JavaScript中,它将做得更好。 Something like this: 像这样:

 var button = document.getElementById("button"); var target = document.getElementById("target"); button.innerHTML = "Close"; button.onclick = function() { hide(target); }; function hide(target) { console.log("hiding"); target.style.display = "none"; console.log(target); button.innerHTML = "Open"; button.onclick = function() { show(target); }; } function show(target) { target.style.display = "block"; button.innerHTML = "Close"; button.onclick = function() { hide(target); }; } 
 <button id="button">Close</button> <div id="target">Hello World!</div> 

Here is another answer in case you want to use toggle on multiple buttons. 如果您想在多个按钮上使用toggle ,这是另一个答案。 This solution uses the button to store the state of its target, and so it uses toggle.call() to modify the value of this within toggle . 此解决方案使用该按钮来存储其目标的状态,并且因此它使用toggle.call()修改的值thistoggle

It's inspired by jQuery.toggle() and Yuriy Yakym's answer . 它的灵感来自jQuery.toggle()Yuriy Yakym的答案

 for (i = 1; i <= 3; i++) { var button = document.getElementById("button"+i), target = document.getElementById("target"+i); // wrapped in closure because https://stackoverflow.com/q/8909652/5743988 (function(_button, _target) { _button.onclick = function() { toggle.call(_button, _target); } })(button, target); } function toggle(target) { this.func = this.func || hide; // if "func" is undefined, assign "hide" this.func.call(target); // execute "func" this.func = this.func === hide // make "func" call the other thing next time ? (this.innerHTML = "Show", show) : (this.innerHTML = "Hide", hide); } function hide() { this.oldDisplay = window.getComputedStyle(this).display; this.style.display = "none"; // better than "height = 0px" } function show() { this.oldDisplay = this.oldDisplay || "block"; this.style.display = this.oldDisplay; } 
 button, div { display: inline-block; } 
 <button id="button1">Close</button> <div id="target1">Hello World!</div> <br> <button id="button2">Close</button> <div id="target2">Hello Again World!</div> <br> <button id="button3">Close</button> <div id="target3">Hello Again Again World!</div> 

It's possible to save button's state inside its context. 可以将按钮的状态保存在其上下文中。 And then, depending on its state call appropriate functions. 然后,根据其状态调用适当的功能。

 function toggle() { this.state = !this.state; // false - open; true - closed; default = false (because is not initialized yet) this.innerHTML = this.state ? 'Close' : 'Open'; if(this.state) { open(); } else { close(); } } function open() { alert("Opened"); } function close() { alert("Closed"); } 
 <a class="button" onclick="toggle.call(this)">Open</a> <a class="button" onclick="toggle.call(this)">Open</a> <a class="button" onclick="toggle.call(this)">Open</a> 

Either the jQuery toggle or the toggleClass methods should the trick for you. jQuery切换toggleClass方法都可以满足您的需要。

Edit, per requested, an example: 根据要求编辑示例:

Say you have a CSS class that looks like this, based on the OP: 假设您有一个基于OP的CSS类,如下所示:

.info-box { 
    height: 300px;
}

By using $('#myTarget').toggleClass('info-box') , you will be removing that class or adding it. 通过使用$('#myTarget').toggleClass('info-box') ,您将删除该类或将其添加。 If you don't have it, you'll add it. 如果没有它,则将其添加。 If you do have it, you'll remove it. 如果有,则将其删除。 Just like that. 就这样

So, a working example I just threw together is here, and it implements toggle() by itself without the need to add any classes, which I believe still accomplishes what the OP was seeking resolution for. 因此,这里有一个我刚刚提出的工作示例,它无需添加任何类toggle()实现toggle() ,我相信这仍可以完成OP所寻求的解决方案。

https://jsfiddle.net/bbh9m478/1/ https://jsfiddle.net/bbh9m478/1/

Hope that helps! 希望有帮助!

I figured out a straightforward solution for this. 我想出了一个简单的解决方案。

When the open link is clicked and the div opens I need the open link to disappear and a close link displayed as a separate element. 单击打开链接并打开div时,我需要打开链接消失并且关闭链接显示为单独的元素。 The same event should work in reverse ie close link closes div and displays open link. 相同事件应反向运行,即关闭链接关闭div并显示打开链接。

While the original code I am working with uses an inline height property to control the open and close, the example below uses display property. 当我使用的原始代码使用内联height属性控制打开和关闭时,以下示例使用display属性。

function show(target) {
    document.getElementById(target).style.display = 'block';
}
function hide(target) {
    document.getElementById(target).style.display = 'none';
}
function hideButton() {
    var x = document.getElementById("open");
    x.style.display = "none";
    var x = document.getElementById("close");
    x.style.display = "";
}
function showButton() {
    var x = document.getElementById("open");
    x.style.display = "";
    var x = document.getElementById("close");
    x.style.display = "none";
}

<a class="button" onclick="show('click-drop');hideButton()" id="open">Open</a>

<div id="click-drop" style="display:none">

<a class="button" onclick="hide('click-drop');showButton()" id="close" style="display:none">Close</a>

</div>              

Here it is in the wild - http://iemajen.com/scripts/mssa/ 这是在野外-http://iemajen.com/scripts/mssa/

With this in place I have two more questions. 有了这个,我还有两个问题。

  1. How would I write this so it uses unobtrusive Javascript to attach behaviour to DOM elements from within the JS solely, meaning separate HTML from JS 我将如何编写它,以便它使用不引人注目的Javascript将行为仅从JS内附加到DOM元素,这意味着将HTML与JS分开
  2. How can I write this to be used on multiple divs? 如何编写可在多个div上使用的代码? I have a dozen divs that will be closed initially and each opened individually using the markup I wrote. 我有一打div,这些div最初将关闭,每个div都使用我编写的标记单独打开。 I could write it up for each div / unique ids and such, but that seems like more bad practice and I would like to learn the best practice. 我可以为每个div /唯一ID等编写代码,但这似乎是更糟糕的做法,我想学习最佳做法。

Should I just open a new topic for the last question? 我应该为最后一个问题打开一个新主题吗?

After you create your element 创建元素之后

var elem = document.querySelector('.js-switch'); var init = new Switchery(elem);

Bind your OnClickMetod to init.switcher like this 像这样将您的OnClickMetod绑定到init.switcher

$(init.switcher).on('click', OnClickMetod);

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

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