Editing description. I'm developing a script. This script download a page for authorization via curl (php). After that I need to click on button ("LOGIN") for entering my login and password.
<li class="login_menu_button"> <a href="javascript:void(0);">LOGIN</a> </li>
I just try to use this code, but it doesn't work
How can I realize click on this button? This button just switch from one form to another (from sign in to login) without page reload. Thanks.
答案 0 :(得分:1)
Just try something like this(Edited javascript:void(0);
present in your code):
$(function(){
$("li a").click(function(e){
e.preventDefault();
var hrefPath = $(this).attr('href');
alert(hrefPath);
//use it in your window.location
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li class="login_menu_button"> <a href="test-page">LOGIN</a> </li>
答案 1 :(得分:1)
There's a mistake in your code getElementByTagName should be getElementsByTagName which returns a array of elements, so you need need to pick the matching element in the array, your better off using the below:
var lnk = document.querySelector(".login_menu_button");
lnk.onclick = function(e){
// your custom logic here
e.preventDefault();
}
it will select your li element and bind the onclick to your handler and prevent the default action of your link.
答案 2 :(得分:1)
对于像这样的事情使用原始javascript(即向DOM元素添加行为)可能不明智
我通常使用jquery。
因此,Jquery检测到链接上的点击就像定义“点击”一样简单。像这样的事件处理程序:
$('li a').click(function(e) {
//handle the click here.
e.preventDefault();
});
干杯