Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript rs,比如onclick事件_Javascript_Jquery_Events_Unobtrusive Javascript - Fatal编程技术网

Javascript rs,比如onclick事件

Javascript rs,比如onclick事件,javascript,jquery,events,unobtrusive-javascript,Javascript,Jquery,Events,Unobtrusive Javascript,下面是我编写的示例代码,您可以看到live()方法如何将所选元素(甚至是新创建的元素)绑定到事件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>

下面是我编写的示例代码,您可以看到live()方法如何将所选元素(甚至是新创建的元素)绑定到事件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/jquery-ui.min.js"></script>

        <input type="button" id="theButton" value="Click" />
        <script type="text/javascript">
            $(document).ready(function()
                {
                    $('.FOO').live("click", function (){alert("It Works!")});
                    var $dialog = $('<div></div>').html('<div id="container"><input type ="button" id="CUSTOM" value="click"/>This dialog will show every time!</div>').dialog({
                                                                                                         autoOpen: false,
                                                                                                         tite: 'Basic Dialog'
                                                                                                     });
                    $('#theButton').click(function()
                    {
                        $dialog.dialog('open');
                        return('false');
                    });
                    $('#CUSTOM').click(function(){
                        //$('#container').append('<input type="button" value="clickmee" class="FOO" /></br>');
                        var button = document.createElement("input");
                        button.setAttribute('class','FOO');
                        button.setAttribute('type','button');
                        button.setAttribute('value','CLICKMEE');
                        $('#container').append(button);
                    });
                    /* $('#FOO').click(function(){
                                                     alert("It Works!");
                                                 }); */
            });
        </script>
    </body>
</html>

无标题文件
$(文档).ready(函数()
{
$('.FOO').live(“单击”,函数(){alert(“它工作!”)});
var$dialog=$('').html('每次都会显示此对话框!')。dialog({
自动打开:错误,
tite:“基本对话框”
});
$(“#按钮”)。单击(函数()
{
$dialog.dialog('open');
返回(“假”);
});
$('#自定义')。单击(函数(){
//$(“#容器”)。追加(“
”); var按钮=document.createElement(“输入”); setAttribute('class','FOO'); setAttribute('type','button'); setAttribute('value','CLICKMEE'); $(“#容器”).append(按钮); }); /*$('#FOO')。单击(函数(){ 警惕(“它起作用了!”); }); */ });
您可以使用live()方法将元素(甚至是新创建的元素)绑定到事件和处理程序,如onclick事件

下面是我编写的示例代码,您可以看到live()方法如何将所选元素(甚至是新创建的元素)绑定到事件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/jquery-ui.min.js"></script>

        <input type="button" id="theButton" value="Click" />
        <script type="text/javascript">
            $(document).ready(function()
                {
                    $('.FOO').live("click", function (){alert("It Works!")});
                    var $dialog = $('<div></div>').html('<div id="container"><input type ="button" id="CUSTOM" value="click"/>This dialog will show every time!</div>').dialog({
                                                                                                         autoOpen: false,
                                                                                                         tite: 'Basic Dialog'
                                                                                                     });
                    $('#theButton').click(function()
                    {
                        $dialog.dialog('open');
                        return('false');
                    });
                    $('#CUSTOM').click(function(){
                        //$('#container').append('<input type="button" value="clickmee" class="FOO" /></br>');
                        var button = document.createElement("input");
                        button.setAttribute('class','FOO');
                        button.setAttribute('type','button');
                        button.setAttribute('value','CLICKMEE');
                        $('#container').append(button);
                    });
                    /* $('#FOO').click(function(){
                                                     alert("It Works!");
                                                 }); */
            });
        </script>
    </body>
</html>

无标题文件
$(文档).ready(函数()
{
$('.FOO').live(“单击”,函数(){alert(“它工作!”)});
var$dialog=$('').html('每次都会显示此对话框!')。dialog({
自动打开:错误,
tite:“基本对话框”
});
$(“#按钮”)。单击(函数()
{
$dialog.dialog('open');
返回(“假”);
});
$('#自定义')。单击(函数(){
//$(“#容器”)。追加(“
”); var按钮=document.createElement(“输入”); setAttribute('class','FOO'); setAttribute('type','button'); setAttribute('value','CLICKMEE'); $(“#容器”).append(按钮); }); /*$('#FOO')。单击(函数(){ 警惕(“它起作用了!”); }); */ });
的文档中有一个很好的解释

简言之:

事件处理程序仅绑定到当前选定的元素;在代码调用
.on()
时,它们必须存在于页面上

因此,在下面的示例中,
#数据表tbody tr
必须在生成代码之前存在

$("#dataTable tbody tr").on("click", function(event){
    console.log($(this).text());
});
如果将新的HTML注入页面,则最好使用委派事件来附加事件处理程序,如下所述

委托事件的优点是,它们可以处理来自后代元素的事件,这些子元素在以后添加到文档中。例如,如果表存在,但行是使用代码动态添加的,则以下操作将对其进行处理:

$("#dataTable tbody").on("click", "tr", function(event){
    console.log($(this).text());
});
除了能够处理尚未创建的子元素上的事件外,委托事件的另一个优点是,当必须监视许多元素时,它们的开销可能会低得多。在
tbody
中有1000行的数据表上,第一个代码示例将处理程序附加到1000个元素

委派事件方法(第二个代码示例)只将事件处理程序附加到一个元素
tbody
,事件只需要向上冒泡一个级别(从单击的
tr
tbody


注意:委派事件不适用于。

的文档中有一个很好的解释

简言之:

事件处理程序仅绑定到当前选定的元素;在代码调用
.on()
时,它们必须存在于页面上

因此,在下面的示例中,
#数据表tbody tr
必须在生成代码之前存在

$("#dataTable tbody tr").on("click", function(event){
    console.log($(this).text());
});
如果将新的HTML注入页面,则最好使用委派事件来附加事件处理程序,如下所述

委托事件的优点是,它们可以处理来自后代元素的事件,这些子元素在以后添加到文档中。例如,如果表存在,但行是使用代码动态添加的,则以下操作将对其进行处理:

$("#dataTable tbody").on("click", "tr", function(event){
    console.log($(this).text());
});
除了能够处理尚未创建的子元素上的事件外,委托事件的另一个优点是,当必须监视许多元素时,它们的开销可能会低得多。在
tbody
中有1000行的数据表上,第一个代码示例将处理程序附加到1,00
$(document).on('mouseover mouseout', '.dosomething', function(){
    // what you want to happen when mouseover and mouseout 
    // occurs on elements that match '.dosomething'
});
$('.buttons').on('click', 'button', function(){
    // do something here
});
<div class="buttons">
    <!-- <button>s that are generated dynamically and added here -->
</div>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/jquery-ui.min.js"></script>

        <input type="button" id="theButton" value="Click" />
        <script type="text/javascript">
            $(document).ready(function()
                {
                    $('.FOO').live("click", function (){alert("It Works!")});
                    var $dialog = $('<div></div>').html('<div id="container"><input type ="button" id="CUSTOM" value="click"/>This dialog will show every time!</div>').dialog({
                                                                                                         autoOpen: false,
                                                                                                         tite: 'Basic Dialog'
                                                                                                     });
                    $('#theButton').click(function()
                    {
                        $dialog.dialog('open');
                        return('false');
                    });
                    $('#CUSTOM').click(function(){
                        //$('#container').append('<input type="button" value="clickmee" class="FOO" /></br>');
                        var button = document.createElement("input");
                        button.setAttribute('class','FOO');
                        button.setAttribute('type','button');
                        button.setAttribute('value','CLICKMEE');
                        $('#container').append(button);
                    });
                    /* $('#FOO').click(function(){
                                                     alert("It Works!");
                                                 }); */
            });
        </script>
    </body>
</html>
$("#dataTable tbody tr").on("click", function(event){
    console.log($(this).text());
});
$("#dataTable tbody").on("click", "tr", function(event){
    console.log($(this).text());
});
document.addEventListener('click', function (e) {
    if (hasClass(e.target, 'bu')) {
        // .bu clicked
        // Do your thing
    } else if (hasClass(e.target, 'test')) {
        // .test clicked
        // Do your other thing
    }
}, false);
function hasClass(elem, className) {
    return elem.className.split(' ').indexOf(className) > -1;
}
function hasClass(elem, className) {
    return elem.classList.contains(className);
}
var myElement = $('<button/>', {
    text: 'Go to Google!'
});

myElement.bind( 'click', goToGoogle);
myElement.append('body');


function goToGoogle(event){
    window.location.replace("http://www.google.com");
}
$(document).on("click", 'selector', function() {
    // Your code here
});
$(document.body).on('click','.element', function(e) {  });
 $(document.body).on('click','.element *', function(e) {  });
$(document.body).on('click','.#element_id > element', function(e) {  });
<div class="container">
     <ul class="select">
         <li> First</li>
         <li>Second</li>
    </ul>
</div>
$(document).ready(function(e) {
    $('.container').on( 'click',".select", function(e) {
        alert("CLICKED");
    });
 });
$(document).on( 'click', '.click-activity', function () { ... });
$('.buttons').on('click', 'button', function(){
    // your magic goes here
});
$('.buttons').delegate('button', 'click', function() {
    // your magic goes here
});
// creating a dynamic element (container div)
var $div = $("<div>", {id: 'myid1', class: 'myclass'});

//creating a dynamic button
 var $btn = $("<button>", { type: 'button', text: 'Click me', class: 'btn' });

// binding the event
 $btn.click(function () { //for mouseover--> $btn.on('mouseover', function () {
    console.log('clicked');
 });

// append dynamic button to the dynamic container
$div.append($btn);

// add the dynamically created element(s) to a static element
$("#box").append($div);
<html>
    <head>
        <title>HTML Document</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    </head>

    <body>
        <div id="hover-id">
            Hello World
        </div>

        <script>
            jQuery(document).ready(function($){
                $(document).on('mouseover', '#hover-id', function(){
                    $(this).css('color','yellowgreen');
                });

                $(document).on('mouseout', '#hover-id', function(){
                    $(this).css('color','black');
                });
            });
        </script>
    </body>
</html>
const sendAction = function(e){ ... }
// bind the click
$('body').on('click', 'button.send', sendAction );

// unbind the click
$('body').on('click', 'button.send', function(){} );
$(document).on("click", "selector", function() {
    // Your code here
});
$('.wrapper-class').on("click", '.selector-class', function() {
    // Your code here
});
<div class="wrapper-class">
    <button class="selector-class">
      Click Me!
    </button>
</div>