我想为稍后在DOM中创建的元素添加事件句柄.
基本上,我想要做的是,当我点击时p#one
,p#two
将创建新元素,然后我点击p#two
,告诉我"p#two"点击.但是,它不起作用,我console.log
点击后没有得到'p#two clicked' 的结果p#two
.
我on()
用来添加点击事件p#two
.我做错了什么?
谢谢.
以下是我的示例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>on() test</title>
<link type="text/css" href="http://localhost/jquery-ui-1.8.20.custom/css/smoothness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="http://localhost/jquery-ui-1.8.20.custom/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://localhost/jquery-ui-1.8.20.custom/js/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('p#two').on('click', function() {
console.log('p#two clicked');
});
$('p#one').click(function() {
console.log('p#one clicked');
$('<p id="two">two</p>').insertAfter('p#one');
});
}); // end doc ready
</script>
</head>
<body>
<p id="one">one</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) HTML:
<div id="search">
<input id="term" type="text" value="enter your search" />
<button id="hit" type="button" name="">Search</button>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$(document).ready(function() {
var term = $('#term').val();
$('#hit').click(function() {
alert(term);
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,无论我在输入字段中键入什么内容,然后点击按钮,它始终会提醒原始输入值"输入您的搜索".
我该如何解决?
HTML:
<span>hello world!</span>
Run Code Online (Sandbox Code Playgroud)
js :(使用回调)
$('span').click(function() {
$(this).animate({
fontSize: '+=10px'
}, 'slow', function() {
// callback after fontsize increased
$(this).text( $(this).text() + ' rolled! ' );
});
});
Run Code Online (Sandbox Code Playgroud)
因此,每次SPAN
单击时,在字体大小增加后附加文本"滚动",而不是一起发生.
它可以通过使用queue()来完成,如下所示:
js :(使用queue())
$('span').click(function() {
$(this).animate({
fontSize: '+=10px'
}, 'slow'})
.queue(function() {
$(this).text( $(this).text() + ' rolled! ' );
});
});
Run Code Online (Sandbox Code Playgroud)
我不确定它们之间有什么区别.都做同样的事情.
为什么queue()比使用回调更好/更喜欢(或者为什么不是)?queue()有什么特别之处?
谢谢.
在浏览一些Web服务文档时,我遇到了" payload "," xml payload " 这个词......
来自维基百科:"*计算中的有效负载(有时称为实际或正文数据)是数据传输的货物.*"
这里没有多大意义.任何人都可以解释有效载荷/ xml有效载荷的含义 谢谢.
查询: SELECT id, name, FROM users u WHERE **id <> 0** LIMIT 50 OFFSET 0
这条款id <> 0
在这里意味着什么?这是不是意味着:
id小于零或id大于零
我经常看到代码是一个没有可见性关键字的函数.例如:
class Foo() {
function bar() {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
它是public
功能的简写吗?省略它是一个好习惯吗?
class Foo() {
public function bar() {
//..
}
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找与支持Google OAuth 2.0的API一起使用的所有可能范围值的列表,例如:
https://www.googleapis.com/auth/urlshortener
https://www.googleapis.com/auth/tasks
Run Code Online (Sandbox Code Playgroud)
我迷失在Google API文档中,无法找到包含此类信息的页面.我在哪里可以找到它?
谢谢.
// Define a walk_the_DOM function that visits every
// node of the tree in HTML source order, starting
// from some given node. It invokes a function,
// passing it each node in turn. walk_the_DOM calls
// itself to process each of the child nodes.
var walk_the_DOM = function walk(node, func) {
func(node);
node = node.firstChild;
while (node) {
walk(node, func);
node = node.nextSibling;
}
};
// Define a getElementsByAttribute function. It
// takes an attribute name string and an …
Run Code Online (Sandbox Code Playgroud) 根据jQuery load()方法api:
.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] )
Run Code Online (Sandbox Code Playgroud)
通过下面的工作示例
$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
});
Run Code Online (Sandbox Code Playgroud)
它提供'url'和'callback function'的参数,跳过[data]参数.
示例代码不应该将回调函数视为[data]参数(第二个参数)吗?由于API中定义的参数的顺序.通过遵循API,第一个是url,第二个是数据,第三个是回调.
我不明白为什么代码会起作用.非常困惑.
可能重复:
jquery on vs click方法
来自API:
.on():将一个或多个事件的事件处理函数附加到所选元素.
示例代码:
$("#dataTable tbody tr").on("click", function(event){
alert($(this).text());
});
Run Code Online (Sandbox Code Playgroud)
我们可以写下这样的东西:
$("#dataTable tbody tr").click(function() {
alert($this).text();
});
Run Code Online (Sandbox Code Playgroud)
为什么要在.on()中包装事件呢?
谢谢.
javascript ×5
jquery ×5
php ×2
google-api ×1
oauth-2.0 ×1
payload ×1
queue ×1
rest ×1
soap ×1
sql ×1
visibility ×1
xml ×1