如何在< a标签上使用$(document).on(" click ..? [英] How to use $(document).on("click.. on <a tag?

查看:89
本文介绍了如何在< a标签上使用$(document).on(" click ..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,我正在使用jQuery 1.7.1来学习Knockout JS,我正在观看作者的视频演示.在他的示例中,他有一个类似

I am new to jQuery and I am using jQuery 1.7.1 to learn Knockout JS, I was following a video demo by the author. In his example he has a tag something like

<a href="#" class="button-delete">Delete</a>

在视图模型中,他有类似的东西

and in the viewmodel he has something like

$(document).on("click", ".button-delete", function() { console.log("inside"); });

当我在旁边单击删除按钮尝试时,我从没看到console.log显示在Chrome F12屏幕的控制台窗口中.我在这里有两个部分的问题

When I tried in my side when I click on the delete button I never see the console.log show up on the console window of the Chrome F12 screen. I have two part question here

  1. 我在做错什么阻止了控制台消息的显示吗?
  2. 如果我没有可做CSS的类,是否还有其他方法可以在viewmodel中执行相同的任务?

我更正了我的拼写错误,该代码具有正确的括号(我使用网络矩阵,因此可以解决这些问题).这是我的html

edit: I corrected my typo, the code has proper parenthesis (I use web matrix so it take care of those issues). Here is my html

<!DOCTYPE html>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="Scripts/knockout-2.0.0.js" type="text/javascript"></script>
<script src="Scripts/ViewModel.js" type="text/javascript"></script>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
</head>
<body onload="init()">
    <input data-bind="value: tagsToAdd"/>
    <button data-bind="click: addTag">Add</button>
   <ul data-bind="foreach: tags">
           <li>
               <span data-bind="text: Name"></span>
               <div>
                   <a href="#" class="btn btn-danger" >Delete</a>
               </div>
           </li>
   </ul>
</body>
</html>

这是我的剔除视图模型

/// <reference file="jquery-1.7.1.js" />
/// <reference file="knockout-2.0.0.js" />

var data = [
   {Id: 1, Name: "Ball Handling" },
   {Id: 2, Name: "Shooting" },
   {Id: 3, Name: "Rebounding" }
];

function viewModel()
{
    var self = this;    
    self.tags = ko.observableArray(data);
     self.tagsToAdd = ko.observable("");

    self.addTag = function() {
       self.tags.push({ Name: this.tagsToAdd() });
       self.tagsToAdd("");
    }
}


$(document).on("click", ".btn-danger", function () {
    console.log("inside");
    });


 var viewModelInstance;
function init(){
    this.viewModelInstance = new viewModel();
    ko.applyBindings(viewModelInstance);    
}

推荐答案

好像您已经有了第一个答案.如果没有类名,则在其他方式"上绑定单击事件;有一些选项.您可以在标签中添加一个ID,然后以这种方式进行调用.或者,如果您不想添加类或id,则可以将数据绑定语法与"click"内置绑定一起使用,以在视图模型上调用方法(显然,这不是jquery evnet样式,因此由您决定如何组织活动).像这样:

Looks like you got your first answer already. On the "other ways" to bind the click event if you don;t have a class name, there are a few options. You could add an id to the tag, and call it that way. Or if you don't want to add a class nor an id, you can use the data-bind syntax with the "click" built-in binding to invoke a method on your view model (obviously this is not the jquery evnet style, so its up to you how you want to wire up your events). Like this:

<button data-bind="click: someMethod">Click me</button>

这篇关于如何在&lt; a标签上使用$(document).on(&quot; click ..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆