1

嗨,我有一些工作 jquery 代码是:

$(document).ready(function()
{
$("#createaccount").submit(function(e){
    var fname = $("#fname").val();
    var surname = $("#surname").val();
    var email = $("#email").val();
    var location = $("#location").val();
    var username = $("#username").val();
    var password = $("#password").val();
    var re_pass = $("#re_password").val();

    var valid = true;

    if(fname == ""){
        $("#fnameerror").html('<span class="errortext">Please enter your first name</span>');
        valid = false;
    }

    if(surname == ""){
        $("#surnameerror").html('<span class="errortext">Please enter your surname</span>');
        valid = false;

    }

    if(email == ""){
        $("#emailerror").html('<span class="errortext">Please enter an email address</span>');
        valid = false;

    }
    else{
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(reg.test(email)==false){
            $("#emailerror").html('<span class="errortext">The email address is invalid</span>');
            valid = false;
        }
    }

    if(location == ""){
        $("#locationerror").html('<span class="errortext">Please enter your location</span>');
        valid = false;
    }

    if(username == ""){
        $("#usernameerror").html('<span class="errortext">Please enter a username</span>');
        valid = false;
    }
    else{
        if(username.length < 6){
            $("#usernameerror").html('<span class="errortext">Username must be at least 6 characters long</span>');
            valid = false;
        }
    }

    if(password == ""){
        $("#passworderror").html('<span class="errortext">Please enter a password</span>');
        valid = false;
    }
    else{
        if(password.length < 6){
            $("#passworderror").html('<span class="errortext">Password must be at least 6 characters long</span>');
            valid = false;
        }
    }

    if(re_pass == ""){
        $("#re_passerror").html('<span class="errortext">Please re-enter your password</span>');
        valid = false;
    }
    else{
        if(re_pass != password){
            $("#re_passerror").html('<span class="errortext">The passwords do not match</span>');
            valid = false;
        }
    }

    if(valid == false){
        return false;
    }
    else{
        $.ajax({
            type: 'POST',
            url: 'createaccount2.php',
            data: $("#createaccount").serialize(),
            success: function(response){

                if(response == "1"){
                    $("#response").html('<span class="responsefail">Your username has already been taken, please enter another</span>');
                }
                else if(response == "2"){
                    $("#response").html('<span class="responsefail">An account is already registered to that email</span>');
                }
                else if(response == "3"){
                    $("#response").html('<span class="responsegood">Your account has been created!</span>');
                    $("#redirect").html('<span class="redirect">Click</span><a class="redirectlink" href="divbayindex.htm">HERE</a><span class="redirect"> to return to the DiveBay homepage</span>');
                }
                else if(response == "4"){
                    $("#response").html('<span class="responsefail">Something went wrong, your account could not be created, please try again in a moment</span>');
                    $("#redirect").html('<span class="redirect">Click</span><a class="redirectlink" href="divbayindex.htm">HERE</a><span class="redirect"> to return to the DiveBay homepage</span>');

                }
            }

        });
    e.preventDefault();
    }
    });





});

我只是想知道如何为这个跨度添加样式。我在原始页面的 css 文件中包含了这些 span 类的 css,但是当这个 span 被函数加载时,它会显示为默认值。

我在加载搜索结果表的另一个页面上遇到了类似的问题,它只显示为默认值。我认为它与外部 css 参考有关?

4

2 回答 2

0

您不必做任何特定的事情来使 CSS 应用于创建的跨度。只需像使用任何其他类一样设置它的样式,例如:

.redirect {
  color: #EEE;
  font-size: 15px;  
}

.responsefail {
  color: #FF0000;
  font-size: 20px;
}

然后,这些类的样式将与新插入的元素一起加载,即使样式表在创建之前就已加载。

但是,如果您希望使用 jquery 添加 css,则可以继续添加

$('.yourclass').css('attribute', 'value');

在每个 if 动作中。

如果这对您没有进一步帮助,请提供该页面的 html 代码。

于 2012-08-08T08:21:02.207 回答
0

CSS 规则应该适用于屏幕上的所有元素,而不仅仅是页面首次加载时存在的元素。我会检查 css 选择器以确保它们与类名匹配(确保你记住了类之前的 .)

于 2012-08-08T08:13:15.760 回答