我有一些代码,我循环遍历页面上的所有选择框,并将.hover事件绑定到它们,以便在它们的宽度上做一些麻烦mouse on/off.
这发生在页面准备就绪并且工作得很好.
我遇到的问题是,我在初始循环后通过Ajax或DOM添加的任何选择框都不会受到事件限制.
我找到了这个插件(jQuery Live Query Plugin),但在我用插件添加另外5k到我的页面之前,我想知道是否有人知道这样做的方法,无论是直接使用jQuery还是通过其他选项.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
$("h2").html("<p class='test'>click me</p>")
});
$(".test").click(function(){
alert();
});
});
</script>
</head>
<body>
<h2></h2>
<button>generate new element</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我试图生成类名称的新标签test中<h2>单击按钮.我还定义了一个与之关联的点击事件test.但事件不起作用.
有人可以帮忙吗?
我的要求是创建等于json数组计数的按钮数.我成功地在jquery中动态创建按钮.但是没有为点击操作调用jquery的.ready函数中的方法.我试过在SO搜索.找到了一些解决方案,但对我来说没有任 我对jquery很新.请帮忙...
我的代码:jQuery:
$(document).ready(function()
{
currentQuestionNo = 0;
var questionsArray;
$.getJSON('http://localhost/Sample/JsonCreation.php', function(data)
{
questionsArray = data;
variable = 1;
//CREATE QUESTION BUTTONS DYNAMICALLY ** NOT WORKING
for (var question in questionsArray)
{
var button = $("<input>").attr("type", "button").attr("id", "questionButton").val(variable);
$('body').append(button);
//Tried using .next here - but it dint work...
//$('body').append('<button id="questionButton">' + variable + '</button>');
variable++;
}
displayQuestionJS(questionsArray[currentQuestionNo], document);
});
$("button").click(function()
{
if ($(this).attr('id') == "nextQuestion")
{
currentQuestionNo = ++currentQuestionNo;
}
else if ($(this).attr('id') == "previousQuestion")
{
currentQuestionNo = --currentQuestionNo;
} …Run Code Online (Sandbox Code Playgroud) 我有一个JavaScript文件AppForm.js,我希望在成功的ajax post响应后重新初始化.
文件本身包含等等
(function(namespace, $) {
"use strict";
var AppForm = function() {
// Create reference to this instance
var o = this;
// Initialize app when document is ready
$(document).ready(function() {
o.initialize();
});
};
var p = AppForm.prototype;
p.initialize = function() {
// Init events
this._enableEvents();
this._initRadioAndCheckbox();
this._initFloatingLabels();
this._initValidation();
};
p._enableEvents = function () {
//blah blah blah
e.preventDefault();
};
p._initRadioAndCheckbox = function () {
};
p._initFloatingLabels = function () {
};
p._initValidation = function () { …Run Code Online (Sandbox Code Playgroud) 我有一个由php和mySQL填充的多行和多列的表.对于某些td,我在document.ready函数中添加了jQuery click事件,让用户更改内容.
但我也可以选择向表中添加行并手动填充它们.但是由于我添加的行不在文档就绪,因此它们不会附加单击事件处理程序,因此我无法单击它们来获取输入框.
<table>
<tr>
<td class="clickable">Some info</td>
<td class="clickable">Some more info</td>
<td>Unchangable info</td>
</tr>
... more similar rows ...
</table>
Run Code Online (Sandbox Code Playgroud)
然后是jQuery
$("tr.clickable").click(function() {
//add input fields
}
$("span#addNewRow").click(function() {
$("table").append('<tr><td class="clickable"></td> ... </tr>')
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将新DOM对象附加到某个Div并且它可以工作,但不知何故 - 我为这些新附加对象编写的事件没有响应.这是为什么?
我在这里附上一个简单的例子:点击段落应该隐藏的任何段落.然而,对于使用.append添加的段落,它不起作用.
有我的代码:
$(document).ready(function(){
$("#add").click(function(){
$("#containerDiv").append("<p> I should hide as well if you click me </p>");
});
$("p").click(function(){
$(this).hide();
});
});
Run Code Online (Sandbox Code Playgroud) 我在这里有这个网站
正如您所看到的,"图像"和"Web模板"选项卡上有一个微小的图像滑块,图像从右向左滑动,然后消失,然后它出现在父元素的最后(<tr>在我的情况下) .
如果将鼠标悬停在小图像上,则可以在左侧看到它的预览.
到现在为止还挺好.
但是,如果您等到第一张图像再次出现,则悬停事件将不再起作用.
有可能jquery无法在<tr>标记的末尾看到新的附加元素吗?
我有一个简单的表单被附加到容器:
<form action="/something" data-remote="true" method="post">
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
我的理解是rails_ujs.js捕获所有提交事件,因此在插入新表单时我不需要重新附加任何事件.但是,此表单不会被视为远程表单.即使我rails_ujs.js在一般提交事件中放入调试器,此表单也不会触发该事件.所有表单呈现服务器端触发都没问题.
我是否错过了必须将事件附加到动态插入表单的内容?
我有一个PHP脚本生成一个png图像,其中验证码为图像文本.
session_start();
$captchanumber = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz';
$captchanumber = substr(str_shuffle($captchanumber), 0, 8);
$_SESSION["code"] = $captchanumber;
$image = imagecreatefromjpeg("cap.jpg");
$black = imagecolorallocate($image, 160, 160, 160);
$font = '../assets/fonts/OpenSans-Regular.ttf';
imagettftext($image, 20, 0, 35, 27, $black, $font, $captchanumber);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
Run Code Online (Sandbox Code Playgroud)
我想通过jQuery或JavaScript重新加载图像,所以我使用这样的东西:
$(document).ready(function(e) {
$('.captcha').click(function(){
alert('yolo');
var id = Math.random();
$(".captcha").replaceWith('<img class="captcha" src="img/captcha.php?id='+id+'" />');
id ='';
});
});
Run Code Online (Sandbox Code Playgroud)
场:
<img class="captcha" src="img/captcha.php">
Run Code Online (Sandbox Code Playgroud)
作为第一次尝试,它的工作原理,但之后,如果我再次点击该字段,它将不再工作,我不知道为什么.