簡體   English   中英

JQuery觸發單擊事件不起作用

[英]JQuery trigger click event not working

我無法讓以下工作:

$('#elem').trigger('click');

當我運行它時,它不會觸發點擊,但它也不會在控制台中顯示任何錯誤日志。

我也試過以下沒有運氣:

$('#elem')[0].click();

關於可能出現什么問題或解決方案的任何想法?

更新:Jquery正在工作,當我檢查元素時,#elem確實出現(它是一個按鈕的id)

HTML:

<a:button id="elem">My Button</Button>

JQuery的:

P.when('A', 'jQuery').execute(function (A, $) {
    //when this function is called, it should trigger a button click
    triggerButtonClick = function() {
        $('#elem').trigger('click'); 
    };
});

在document.ready函數中嘗試它,然后在文檔准備好時加載所有dom。你可以根據你的要求做這樣的事情。

 $( document ).ready(function() { $('#radio').click(function(){ $('#elem')[0].click(); }) }); 

我試過這個代碼,它的工作原理

<!DOCTYPE html>
 <html>
 <head>
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">     </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("chk1").click(function(){
    $('#usr').trigger('click');
  });
});
</script>
</head>
<body>
<form action="sample.php">
<button id="chk1">click</button>
<input class="form-control" type="submit" id="usr">
</form>
</body>
</html>

 $(document).ready(function () { $('#elem').click(function () { alert('clicked'); $(this).css('color', 'red'); }); $('#elem').trigger('click'); }); 
 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <body> <div id="elem" >test</div> </body> </html> 

這段代碼適合我:

$(window).load(function() {
    $('#menu_toggle').trigger('click');   
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM