简体   繁体   English

第二次单击后立即执行Javascript

[英]Javascript executes just after second click

I have not found neither a direct nor general answer to this problem but I believe there might be a general interest in solving it once and for all. 对于这个问题,我没有找到直接或普遍的答案,但我相信一劳永逸地解决它可能会引起普遍的兴趣。 I must admit I'm quite new to js. 我必须承认我对js还是很陌生。

My problem is, that I need to click twice on "link" to execute the javascript. 我的问题是,我需要在“链接”上单击两次以执行javascript。

head: 头:

<script type="text/javascript">
function toggle(navi){
var elem = document.getElementById(navi);

if(elem.style.top == "-604px"){
    elem.style.top = "0px"; // elem.style.textDecoration = "underline"; 
}else{
    elem.style.top = "-604px";//elem.style.textDecoration = "none";
}}
</script>

body 身体

<a href="javascript:toggle('navi')">Link</a>

A fiddle for better understanding: fiddle 为了更好理解的小提琴小提琴

Maybe you can help me out.. 也许你可以帮帮我。

you might want to do this using jQuery, then you only have to click once: 您可能要使用jQuery进行此操作,则只需单击一次即可:

<a id='clicketyclick'>Link</a>

<script>
    $("#clicketyclick").click(function() {
        toggle(navi);
    });
</script>

If you're new to JS you should learn jQuery, since it is one of the best javascript frameworks around, and it will make your life quite a lot easier. 如果您是新的JS,你应该学习jQuery,因为它是最好的JavaScript框架之一左右,而且它会使你的生活很轻松了许多。

If you,however, don't want to use jQuery, you could basically use: 但是,如果您不想使用jQuery,则基本上可以使用:

<a onclick="toggle(navi)">Link</a>

Firstly you were not passing a string into the 'toggle' function for it to select the element by it's id. 首先,您没有将字符串传递给'toggle'函数,以便它通过id来选择元素。

Then getting the CSS 'top' property using JQuery seems to solve the issue of 'elem.style.top' being empty on initialisation (requiring double click). 然后,使用JQuery获得CSS“ top”属性似乎可以解决“ elem.style.top”在初始化时为空的问题(需要双击)。

Here's the working fiddle: http://jsfiddle.net/gkhBM/ 这是工作的小提琴: http : //jsfiddle.net/gkhBM/

$("#clicketyclick").click(function() { toggle("navi"); });

function toggle(navi) {
    var elem = document.getElementById(navi);
    //console.log(elem.style.top);
    //console.log($(elem).css('top'));
    if($(elem).css('top') == "-604px"){
        $(elem).css('top',"0px"); // elem.style.textDecoration = "underline"; 
    }else{
        $(elem).css('top',"-604px");//elem.style.textDecoration = "none";
    }
}

console.log is extremely useful when debugging problems. 当调试问题时,console.log非常有用。

You could do as Max Langarek says or you could also just but click as a button instead of an a tag. 您可以按照Max Langarek所说的做,也可以只单击按钮而不是标签。

Maybe something like 也许像

<button  onclick="toggle(navi)">Button</button>

Either way, I've not seen the javascript:toggle(navi) way of calling a function before.. maybe I need more experience. 无论哪种方式,我之前都没有看过javascript:toggle(navi)调用函数的方式..也许我需要更多经验。

Try counting your clicks 尝试计算您的点击次数

var clickNumber = 1;

function functionTwice() {

    if(clickNumber == 1) {
        clickNumber = clickNumber +1;
    }
    else if(clickNumber == 2) {
        clickNumber = 1;
            var elem = document.getElementById(navi);

    if(elem.style.top == "-604px"){
        elem.style.top = "0px"; // elem.style.textDecoration = "underline"; 
    }else{
        elem.style.top = "-604px";//elem.style.textDecoration = "none";
    }
    }


}

HTML: HTML:

<a id='clicketyclick' onClick="functionTwice()">Link</a>
<div id="navi"></div>

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

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