首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jQuery prepend()不工作,有什么问题吗?

jQuery prepend()不工作,有什么问题吗?
EN

Stack Overflow用户
提问于 2014-08-28 11:28:47
回答 1查看 1.5K关注 0票数 1

我很困惑我的append()没有出现在HTML中。你们能告诉我下面的代码有什么问题吗?

代码语言:javascript
运行
复制
<div class="form-group" id="password">
     <label class="control-label" for="inputError"> Password:</label>
     <input type="password" class="form-control" id="input-password" placeholder="Password" value="" name="password"/>
</div>

我的js:

代码语言:javascript
运行
复制
<script type="text/javascript">
        $('input[name="password"]').focusout(function() {
            if($(this).val()==""){
                $('#password').attr('class','form-group has-error');
                $('#password .control-label').prepend("<span class='fa fa-times-circle-o'></span>");
                $('#password .control-label').text(' Input your new password!');
            }
        });
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-28 11:31:52

假设触发了焦点事件,问题是在.prepend()之后使用.prepend()将删除.prepend()添加的内容

代码语言:javascript
运行
复制
$('input[name="password"]').focusout(function () {
    if ($(this).val() == "") {
        $('#password').attr('class', 'form-group has-error');
        $('#password .control-label').text(' Input your new password!');
        $('#password .control-label').prepend("<span class='fa fa-times-circle-o'></span>");
    }
});

演示:小提琴

所以

代码语言:javascript
运行
复制
jQuery(function($){
    $('input[name="password"]').focusout(function () {
        if ($(this).val() == "") {
            $('#password').attr('class', 'form-group has-error');
            $('#password .control-label').text(' Input your new password!').prepend("<span class='fa fa-times-circle-o'></span>");
            //$('#password .control-label').html('<span class="fa fa-times-circle-o"></span> Input your new password!');
        }
    });
});
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25547714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档