javascript - jquery 索引值的问题
阿神
阿神 2017-04-11 12:35:42
[JavaScript讨论组]

有两个input 两个p,如何点击input对应的p显示出来,js的话我懂写,刚接触jquery,请指教一下

账号:

支持中文、字母、数字,4-20个字符

密码:

4-20个字符

阿神
阿神

闭关修行中......

全部回复(5)
PHP中文网
// 给input提示加了一下className
<form>
    账号:
    <input class="input-content" type="text" />
    <p class="input-tips">支持中文、字母、数字,4-20个字符</P>
    密码:
    <input class="input-content" type="password" />
    <p class="input-tips">4-20个字符</P>
</form>

$('.input-content').on({
    focus: function() {
        $(this).next('.input-tips:first').show();
    },
    blur: function() {
        $(this).next('.input-tips:first').hide();
    }
});

// 题主希望的通过索引实现
$('.input-content').each(function(i, v) {
    $(v).on({
        focus: function() {
            $('form p').eq(i).show();
        },
        blur: function() {
            $('form p').eq(i).hide();
        }
    });
});
伊谢尔伦
$("input").on("click",functioin(){
   var index= $(this).index();
   $("p").addClass("hidden");
   $("p").eq(index).removeClass("hidden");

})
巴扎黑

$("input").on("click",functioin(){
$("p").hide().eq($(this).index()).show();
})

PHPz

<form>
账号: <input type="text"/>
<p style="display: none;">支持中文、字母、数字,4-20个字符</P>
密码: <input type="password"/>
<p style="display: none;">4-20个字符</P>
</form>

$(function(){

    $('input').on('focus',function(){
       $(this).next().show();
    }).on('blur',function(){
        $(this).next().hide();
    })
})  
高洛峰

遍历(each)取到input相应的index
jQuery("input").each(function(index,el){
jquery(el).on("click",function(){
jQuery("p").addClass("hidden");
jQuery("p").eq(index).removeClass("hidden");
});
});

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号