Javascript函数只工作一次
我的页面上有一个内联脚本:Javascript函数只工作一次,javascript,jquery-ui,Javascript,Jquery Ui,我的页面上有一个内联脚本: <script type="text/javascript"> var rootPath = '<%= Url.Content("~/") %>'; $(document).ready(function () { $('#logonStatus').click(function () { loadLoginForm(); }); alert('Document Ready'); });
<script type="text/javascript">
var rootPath = '<%= Url.Content("~/") %>';
$(document).ready(function () {
$('#logonStatus').click(function () { loadLoginForm(); });
alert('Document Ready');
});
function loadLoginForm() {
if(!serenity.tools.isStyleSheetLoaded('redmond.css')) {
$('head').append('<%= Url.StyleTag("Redmond/redmond.css", MediaTypes.Screen) %>');
}
if(!serenity.tools.elementExists($('#logonContainer'))) {
$.ajax({
async: false,
cache: false,
datatype: 'html',
success: function (data) { $('body').append(data); },
type: 'GET',
url: '/Membership/LogOn'
});
}
$('#logonContainer').dialog({
modal: true,
hide: 'slide'
});
}
</script>
var serenity = new function () {
$(document).ready(function () {
jQuery.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}
});
});
this.tools = new function () {
this.isStyleSheetLoaded = function (fileName) {
$(document.styleSheets).each(function () {
if (this.href.toLowerCase().indexOf(fileName) != -1) {
this.isStyleSheetLoaded = true;
return;
}
});
this.isStyleSheetLoaded = false;
}
this.elementExists = function (element) {
this.elementExists = element.length != 0;
}
}
}
ajax调用加载的文件只是一个div,其中包含一个包含输入元素的表。该文件中不包含任何javascript
我的问题是,我第一次调用isStyleSheetLoaded时,它工作正常,但在加载文件、显示并关闭对话框后,我单击触发loadLoginForm函数的链接,但这次它说isStyleSheetLoaded不是一个函数。这在所有浏览器中都会出现,所以我99%确定这是我的问题,但我不知道这是什么。有人能给我指一下正确的方向吗
提前谢谢。我认为您的问题如下: 您定义了一个函数“this.isStyleSheetLoaded=function(fileName)”,但在他的主体中,您覆盖了这个属性“this.isStyleSheetLoaded=true;” 因此,在您第一次调用isStyleSheetLoaded之后,该函数将使用boolen进行重载 正确的方法可以是:
this.isStyleSheetLoaded = function (fileName) {
$(document.styleSheets).each(function () {
if (this.href.toLowerCase().indexOf(fileName) != -1) {
return true;
}
});
return false;
}
我认为你的问题如下: 您定义了一个函数“this.isStyleSheetLoaded=function(fileName)”,但在他的主体中,您覆盖了这个属性“this.isStyleSheetLoaded=true;” 因此,在您第一次调用isStyleSheetLoaded之后,该函数将使用boolen进行重载 正确的方法可以是:
this.isStyleSheetLoaded = function (fileName) {
$(document.styleSheets).each(function () {
if (this.href.toLowerCase().indexOf(fileName) != -1) {
return true;
}
});
return false;
}
就是这样,非常感谢。我已经好几年没有接触javascript了,在另一个函数中返回值时遇到问题,我进行了搜索,发现一个博客说我必须将方法名设置为返回值。非常感谢,我会尽快接受您的回答。+1同样适用于从
elementExists
返回值。这不是VBscript!就是这样,非常感谢。我已经好几年没有接触javascript了,在另一个函数中返回值时遇到问题,我进行了搜索,发现一个博客说我必须将方法名设置为返回值。非常感谢,我会尽快接受您的回答。+1同样适用于从elementExists
返回值。这不是VBscript!