几个小时后,我尝试制作一个脚本,在设置window.location.hash时显示div。当页面准备就绪时,脚本可以工作,但是当我尝试用jQuery事件.click()调用它时,它不起作用。
代码:
var bodyHeight = $('body').height();
$(asd);
function asd() {
locationHash = window.location.hash.replace(/^#!/, '');
if(locationHash != "") {
$('div#bgLayer').css({
'height': bodyHeight,
'display': 'block'
});
$('div#feedBack').css('display', 'block');
$('div#bgLayer, div#feedBackRight').click(function() {
var clientName = "";
var clientEmail = "";
var clientWebsite = "";
var clientImage = "";
var clientFeedBack = "";
$('div#bgLayer').css('display', 'none');
$('div#feedBack').css('display', 'none');
$('div#feedBacks').css('display', 'block');
$('div#feedBackForm').css('display', 'none');
$('a#showFeedBacks').css('font-weight', 'bold');
$('h3#clientsAboutUs').css('display', 'inline-block');
$('h3#addFeedBackAboutUs').css('display', 'none');
$('div.pages').css('display', 'block');
$('a#addFeedBack').css('font-weight', 'normal');
window.location.hash = '!';
});
if(locationHash == 'addFeedBack') {
$('div#feedBacks').css('display', 'none');
$('h3#clientsAboutUs').css('display', 'none');
$('h3#addFeedBackAboutUs').css('display', 'inline-block');
$('div.pages').css('display', 'none');
$('div#feedBackForm').css('display', 'block');
$('a#showFeedBacks').css('font-weight', 'normal');
$('a#addFeedBack').css('font-weight', 'bold');
$('input#submitFeedBack').attr('disabled', 'disabled');
}
else if(locationHash == 'showFeedBacks') {
$('div#feedBackForm').css('display', 'none');
$('h3#clientsAboutUs').css('display', 'inline-block');
$('h3#addFeedBackAboutUs').css('display', 'none');
$('div.pages').css('display', 'block');
$('div#feedBacks').css('display', 'block');
$('a#addFeedBack').css('font-weight', 'normal');
$('a#showFeedBacks').css('font-weight', 'bold');
}
}
}
$('#addFeedBack-page').click(asd);
#addFeedBack-page是:
<a href="#!addFeedBack" id="addFeedBack-page" class="buttons" title="Click me">Click me</a>
那么如何做到对不对?
祝你好运, 乔治!
答案 0 :(得分:1)
您想在document.ready
事件中绑定点击处理程序。
所以将代码更改为:
var bodyHeight = $('body').height();
$(function () {
asd();
$('#addFeedBack-page').click(asd);
});
function asd() {
// same as you have it...
}
答案 1 :(得分:0)
var bodyHeight = $('body').height();
$(document).ready(function () {
asd();
$('#addFeedBack-page').click(function(){
asd();
});
});
function asd() {
locationHash = window.location.hash.replace(/^#!/, '');
if(locationHash != "") {
$('div#bgLayer').css({
'height': bodyHeight,
'display': 'block'
});
$('div#feedBack').css('display', 'block');
$('div#bgLayer, div#feedBackRight').click(function() {
var clientName = "";
var clientEmail = "";
var clientWebsite = "";
var clientImage = "";
var clientFeedBack = "";
$('div#bgLayer').css('display', 'none');
$('div#feedBack').css('display', 'none');
$('div#feedBacks').css('display', 'block');
$('div#feedBackForm').css('display', 'none');
$('a#showFeedBacks').css('font-weight', 'bold');
$('h3#clientsAboutUs').css('display', 'inline-block');
$('h3#addFeedBackAboutUs').css('display', 'none');
$('div.pages').css('display', 'block');
$('a#addFeedBack').css('font-weight', 'normal');
window.location.hash = '!';
});
if(locationHash == 'addFeedBack') {
$('div#feedBacks').css('display', 'none');
$('h3#clientsAboutUs').css('display', 'none');
$('h3#addFeedBackAboutUs').css('display', 'inline-block');
$('div.pages').css('display', 'none');
$('div#feedBackForm').css('display', 'block');
$('a#showFeedBacks').css('font-weight', 'normal');
$('a#addFeedBack').css('font-weight', 'bold');
$('input#submitFeedBack').attr('disabled', 'disabled');
}
else if(locationHash == 'showFeedBacks') {
$('div#feedBackForm').css('display', 'none');
$('h3#clientsAboutUs').css('display', 'inline-block');
$('h3#addFeedBackAboutUs').css('display', 'none');
$('div.pages').css('display', 'block');
$('div#feedBacks').css('display', 'block');
$('a#addFeedBack').css('font-weight', 'normal');
$('a#showFeedBacks').css('font-weight', 'bold');
}
}
}
答案 2 :(得分:0)
如果您使用jquery更改当前页面的html,
通常我建议你使用
jQuery(document).ready(function () {
$('#addFeedBack-page').live("click",function(){
//action goes here
});
});