如何使用JQuery与母版页? [英] How to use JQuery with Master Pages?
问题描述
我可以简单的例子,只要有不涉及母版页做工精细。所有我想要做的就是点击一个按钮,并将它说的Hello World,在.js文件的JavaScript,使用母版页。任何帮助非常AP preciated:)
I can get simple examples to work fine as long as there's no master page involved. All I want to do is click a button and have it say "hello world" with the javascript in a .js file, using a master page. Any help very much appreciated :)
推荐答案
修改
由于@Adam在评论中指出的,是土生土长的jQuery的机构,基本上做同样的事情在我原来的答复黑客。使用jQuery你可以做
As @Adam points out in the comments, there is a native jQuery mechanism that basically does the same thing as the hack in my original answer. Using jQuery you can do
$('[id$=myButton]').click(function(){ alert('button clicked'); });
我的黑客最初是作为一个工作原型周围ASP.NET和我适应它原来的答案。需要注意的是jQuery的基本上没有引擎盖下同样的事情。我建议使用jQuery的方式,但是,在实施我的黑客。
My hack was originally developed as a Prototype work around for ASP.NET and I adapted it for the original answer. Note that jQuery basically does the same thing under the hood. I recommend using the jQuery way, though, over implementing my hack.
原来的答复留下评论的上下文
当你使用一个母版页,ASP.NET轧液控件对因页面的名称。你需要找出一种方法来找到合适的控制权交给处理程序添加到(假设你将使用JavaScript处理程序)。
When you use a master page, ASP.NET mangles the names of the controls on the dependent pages. You'll need to figure out a way to find the right control to add the handler to (assuming you're adding the handler with javascript).
我用这个功能来做到这一点:
I use this function to do that:
function asp$( id, tagName ) {
var idRegexp = new RegExp( id + '$', 'i' );
var tags = new Array();
if (tagName) {
tags = document.getElementsByTagName( tagName );
}
else {
tags = document.getElementsByName( id );
}
var control = null;
for (var i = 0; i < tags.length; ++i) {
var ctl = tags[i];
if (idRegexp.test(ctl.id)) {
control = ctl;
break;
}
}
if (control) {
return $(control.id);
}
else {
return null;
}
}
然后,你可以这样做:
Then you can do something like:
jQuery(asp$('myButton','input')).click ( function() { alert('button clicked'); } );
在这里你有你的孩子页面上以下
where you have the following on your child page
<asp:Button ID="myButton" runat="server" Text="Click Me" />
这篇关于如何使用JQuery与母版页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!