简体   繁体   English

将<a href=…>onclick</a>转换<a href=…>为按钮</a>

[英]Converting an <a href=…> to a button onclick

I'm following along with a jquery tutorial that gets initialized with a standard href text link. 我将跟随一个用标准href文本链接初始化的jquery教程。 I have everything working. 我一切正常。 As the final step, I want to remove the text link and replace it with a button using its click event. 作为最后一步,我想删除文本链接,并使用其click事件将其替换为按钮。 Here is my code as it exists right now: 这是我现在存在的代码:

//this is the line that I want to replace with a button    
<a href="#login-box" class="login-window">Log In</a> 

// notice how that previous line somehow invokes this next section.
// this is the part that I don't understand how to do with a button
<div id="login-box" class="login-popup"> 
    <a href="#" class="close">
        <img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" />
    </a>

    <form method="post" class="signin" action="#">
        <fieldset class='textbox'>
            <label class="username">
                <span>Username:</span>
                <input id="username" name="username" value="" type="text" autocomplete="on" placeholder="Username">
            </label>
            <label class="password">
                <span>Password</span>
                <input id="password" name="password" value="" type="password" placeholder="Password">
            </label>

            <button class="submit button" type="button">Sign in</button>
            <p><a class="forgot" href="#">Forgot your password?</a></p>        
        </fieldset>    
    </form>
</div>

So, I set up a very simple button, but I have no idea what to put in the onclick event handler. 因此,我设置了一个非常简单的按钮,但是我不知道该在onclick事件处理程序中添加什么。 If this were a simple URL link, no problem. 如果这是一个简单的URL链接,那没有问题。 I would just write a jquery function (or even just a js function). 我只是写一个jquery函数(甚至只是一个js函数)。 But in the context of this code, I have no idea how to invoke it. 但是在此代码的上下文中,我不知道如何调用它。

<div id='login'>
    <button id='login-form' onclick="???"></button>
</div>

I'm a little out of my depth with this one. 我对这一点有些不满。 Can someone point me in the right direction? 有人可以指出我正确的方向吗? Thanks! 谢谢!

EDIT: There seems to be some confusion with what I am trying to accomplish, so let me explain. 编辑:似乎与我要完成的工作有些混淆,所以让我解释一下。 In the existing code, the href on the first line somehow invokes the code contained in the div with the id="login-box" on the next line. 在现有代码中,第一行的href以某种方式调用div中包含的代码,第二行的id =“ login-box”。 I want to achieve the same effect, just with a button instead of a text-based href. 我想通过按钮而不是基于文本的href来达到相同的效果。 Unfortunately, I have never seen code written this way. 不幸的是,我从未见过以此方式编写的代码。 It works correctly with the href, but I would prefer a button for aesthetic purposes. 它可以与href一起正常使用,但出于美观目的,我希望使用按钮。

I tried this, but there is no response, other than from the console: 我试过了,但是除了控制台没有任何反应:

$('#login-form').on('click',function(){
    console.log("here");
    location.href='#login-box';
});

Let me be clear: I know how to write a button that takes a user to a new web page. 让我清楚:我知道如何编写一个将用户带到新网页的按钮。 This is not taking a user to a new web page. 这不会将用户带到新的网页。 It's a callback to the same html code invoking a different section of it. 这是对同一HTML代码的回调,并调用了它的不同部分。 That's why it is confusing. 这就是为什么它令人困惑。

If you want to open a link on button click then try, 如果您想打开按钮上的链接,请点击然后尝试,

<button id='login-form'>Button</button>

Code would be, 代码是

$('#login-form').on('click',function(){
    location.href='your url goes here';
});

And if you want it to open in a new target then try, 如果您希望它在新目标中打开,请尝试,

$('#login-form').on('click',function(){
    window.open('your url goes here', '_blank');
});

Live Demo 现场演示

Try something like this in Jquery: 在Jquery中尝试这样的事情:

$('#login-form').on('click', function(event)) {
   event.preventDefault();
  $(this).parent().append("<button id="replace">Your code for replace this element</button>");
  $(this).remove();
}

If you want to atach an event for the new html element added, you can do this: 如果要为添加的新html元素进行事件,可以执行以下操作:

  $('#login-form').on('click', 'replace', function(event) {});

If this reponse is not ok, you can explain more what you really want to do (I want to remove the text link -> id of the text link, and add the button ...). 如果此响应不正确,则可以进一步解释您真正想做的事情(我想删除文本链接->文本链接的ID,然后添加按钮...)。

If you want to send the form, when you click on some element. 如果要发送表单,请单击某些元素。

$('#somelement').on('click', function(event) {
    event.preventDefault();
    $("form").first().submit();
}):

I figured it out after finally finding this question 我终于找到这个问题后才想通

The way to do it is this: 方法是这样的:

$('#login-form').on('click',function(){
    $("#login-box").show();
});

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

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