【问题标题】:how to change border color of the Input field dynamically in jquery mobile如何在jquery mobile中动态更改输入字段的边框颜色
【发布时间】:2013-07-27 10:45:36
【问题描述】:

我有一个输入字段,我想在其中提供验证,我要更改占位符文本及其颜色,但我也想更改输入字段边框的颜色。 这是html的代码:

<input class="validate[required,custom[onlyLetterSp]] text-input" id="first_name" name="firstName" type="text" placeholder="First Name" maxlength="10" required>
    <input class="validate[required,custom[onlyLetterSp]] text-input" id="last_name" name="LastName" type="text" placeholder="Last Name" required>

这是用于验证和更改占位符颜色的 javascript 代码:

var defaultColor = 'BBBBBB';
var styleContent = 'input:-moz-placeholder {color: #' + defaultColor + ';} input::-webkit-input-placeholder {color: #' + defaultColor + ';}';
var styleBlock = '<style id="placeholder-style">' + styleContent + '</style>';
var randomColor='ff0000';
    // generate new styles and append to the placeholder style block
    styleContent = 'input:-moz-placeholder {color: #' + randomColor + ';} input::-webkit-input-placeholder {color: #' + randomColor + ';}'
     var colorchng='<style id="placeholder-style">' + styleContent + '</style>';
     if(first_name=="" || first_name=="First Name"){

        $("#first_name").append(colorchng);
        $("#first_name").attr("placeholder","First Name can not be blank");
    } if(last_name=="" || last_name=="Last Name"){
         $("#last_name").append(colorchng);
        $("#last_name").attr("placeholder","Last Name can not be blank");
    }else{
            alert("Data Entered");
    }

【问题讨论】:

    标签: html jquery-mobile cordova-2.0.0


    【解决方案1】:

    实现这个:

    工作示例:http://jsfiddle.net/Gajotres/PMrDn/53/

    HTML:

    <input class="validate[required,custom[onlyLetterSp]] text-input" id="first_name" name="firstName" type="text" placeholder="First Name" maxlength="10"/>
    

    Javascript:

    $('#first_name').parent().css('border-color','red');
    

    【讨论】:

    • 嘿,它工作正常,但它使所有占位符都是红色的,我只希望指定的占位符是红色的。
    • 您能告诉我如何更改选择选项的边框颜色吗?我尝试了与您在输入标签中说的相同的方法,但对选择选项不起作用。
    • @Gajotres :它对我有用,谢谢。我是 JQuery 的新手,为什么“border-color : red !important”在这里不起作用????
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多