javascript没有'在jquery.load之后无法工作
javascript doesn't work after jquery .load
我有一个default.aspx
页面,其中包含以下元素:
- 列表视图
- 分区ID=子视图
- 两个复印按钮
当用户单击这些按钮中的任何一个时,我希望复制一些文本并禁用单击的按钮。
我做到了。复制功能正在运行,禁用样式在这两个按钮上也运行良好。
列表视图的每一行都有一个链接。如果用户点击该链接,我想使用jquery .load
函数加载另一个页面subview.aspx
,并将结果放入div ID=subview 中
$('#subView').load('SubView.aspx?');
子视图页面还包含两个复制按钮。这些按钮可以复制正确的文本,但是,当用户单击其中任何一个按钮时,它们都不会被禁用。
我的代码
我在default.aspx
的顶部包含了zeroclipboard.js
。default.aspx中有两个按钮:
<button type="button" id="Button1" class="copyToBtn" type="button" data-clipboard-text="<%#Eval("Telephone")%>" title="Copy Phone">Copy Phone Number</button>
<button type="button" id="Button3" class="copyToBtn" data-clipboard-text="<%#Eval("Surname")%>" title="Copy Surname">Copy Surname</button>
<script type="text/javascript" src="zero/jjjj.js"></script>
这是subview.aspx 中的两个按钮
<button type="button" id="Button1" class="copyToBtn" type="button" data-clipboard-text="<%#Eval("Telephone")%>"title="Copy Phone">Copy Phone Number</button>
<button type="button" id="Button3" class="copyToBtn" data-clipboard-text="<%#Eval("Surname")%>" title="Copy Surname">Copy Surname</button>
<script type="text/javascript" src="zero/jjjj.js"></script>
这是jjj.js
var client = new ZeroClipboard(document.getElementById("Button1"), {
moviePath: "/zero/ZeroClipboard.swf"
});
client.on("load", function (client) {
client.on("complete", function (client, args) {
$('.copyToBtn').each(function () {
$(this).removeAttr('disabled').removeClass('ui-state-disabled');
});
if (this.id == "Button1") {
$("#Button1").attr("disabled", "disabled");
}
else if (this.id == "Button3") {
$("#Button3").attr("disabled", "disabled");
}
alert(this.id);
});
});
var client3 = new ZeroClipboard(document.getElementById("Button3"), {
moviePath: "/zero/ZeroClipboard.swf"
});
client3.on("load", function (client3) {
client3.on("complete", function (client3, args) {
$('.copyToBtn').each(function () {
$(this).removeAttr('disabled').removeClass('ui-state-disabled');
});
if (this.id == "Button1") {
$("#Button1").attr("disabled", "disabled");
}
else if (this.id == "Button3") {
$("#Button3").attr("disabled", "disabled");
}
alert(this.id);
});
});
请注意警报只是在default.aspx上工作,而不是在subview.aspx上认为复制功能在subview和default.aspx中完全正常。
请帮助
我的最佳猜测是问题在于主页中的按钮与子视图中的按钮具有相同的id。javascript不知道要禁用哪个#Button1,是旧的还是新的。js还有许多其他问题。
我认为jfriend00已经回答了这个问题,但让我试一试:如果您正在动态添加新元素,那么当页面本身加载和解析javascript或重新附加处理程序时,您需要将事件处理程序附加到那里。这是一个类似于您想要做的工作示例…(请确保ZeroClipboard.min.js文件和.swf文件位于您正在测试的根目录中,或者相应地更改脚本标记)。
创建一个正文如下的html页面:
<body>
<button type="button" class="clip_button">Copy To Clipboard</button>
<button type="button" class="clip_button">Copy This Too!</button>
<!--new buttons will be appended inside the sublist div-->
<div class="sublist">
</div>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="ZeroClipboard.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
var client;
// Create a function that sets up your Zero Clipboard Instance
var fnSetUpZCB = function () {
// Every time we run this function, it will attach the event handler to the buttons with the class clip_button
client = new ZeroClipboard( $('.clip_button') );
client.on( 'ready', function(event) {
client.on( 'copy', function(event) {
event.clipboardData.setData('text/plain', event.target.innerHTML);
});
// On aftercopy is where we can manipulate the object that was clicked using event.target (in this case to disable the clicked button)
client.on( 'aftercopy', function(event) {
event.target.disabled = true;
console.log('Copied text to clipboard: ' + event.data['text/plain']);
ZeroClipboard.destroy();
//I'm just appending a new button here so you can see that the dynamic elements are indeed getting the handler attached. You could run your load function here.
$('.sublist').append('<button class="clip_button">Copy This Also!</button>');
//And, finally, after we load the new elements, we have to run our set up again to reinitialize any new items.
fnSetUpZCB();
});
});
client.on( 'error', function(event) {
// console.log( 'ZeroClipboard error of type "' + event.name + '": ' + event.message );
ZeroClipboard.destroy();
});
}
$('document').on('load', fnSetUpZCB());
});
</script>
</body>
相关文章:
- 在jQuery .load Ajax之后执行if-statement
- 在.load和.getScript之后的jQuery绑定仅在警报之后有效
- 如何在jQuery.load(html)之后选择jQuery.Treeview中的当前元素
- 如何使 jQuery 语句在 .load() 之后执行
- CSS :悬停在 jquery load();之后不起作用
- 在 .detach() 和 .append() 之后使用 .load() 无法按预期工作
- JS不起作用,即使在使用window.load之后也是如此
- 在.load()之后,jQuery事件触发两次
- 在jquery.load()之后更新dom
- 在change()处理程序之后通过.load()重新加载表时,JQuery丢失初始点击事件
- 重新加载Jquery.live/.load之后的Javascript
- 在lazy load之后在OWL Carousel上添加类
- 如何在.load()之后获得javascript函数的返回值
- 在.load()页面之后执行javascript
- 在.generate()之后和.load()之前传递参数给c3js tooltop
- 只在load()之后运行函数一次
- javascript没有'在jquery.load之后无法工作
- 如何在.load()之后嵌套事件
- 如何在jQuery .load()之后更新DOM
- 在 jQuery 的 ajax.load() 之后,无法将元素附加到新加载的内容中