C军

不玩博客了!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

jQuery获取文件选择输入框的扩展名

var file=$("input[name='file']").val()
var filename=file.replace(/.*(\/|\\)/, "");  //文件名
var fileExt=(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';  //扩展名

今天在用ajaxfileupload时,遇到一个要刷新一次页面才能再次上传,用live()方法来绑定<input type="file">的change事件就能够解决?直接$("xxx").change();只能调用一次,据闻是IE浏览器的安全性。后来终于找到解决方案了。IE浏览器下<input type="file">选择完成自动提交的问题,在每次处理完成后把<input type="file" />替换成原来的代码,然后随便加个不同的属性。如本例中添加了title。

    var count = -1;
    $("#upload").live("change", function () {
        count++;
    $.ajaxFileUpload(config);
    $("#upload").replaceWith("<input type='file' id='upload' name='upload' style='position:relative; top:0px; left:-240px; width:346px; height:46px; opacity:0; filter: Alpha(Opacity=0); cursor:pointer; title=" + count + "' />");
})    

 

jQuery根据生日计算年龄,星座,生肖的实例:

<html>
<head> <title></title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> //根据输入的生日自动获取星座,生肖和年龄。 var year = new Array("猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗"); jQuery(function () { $("#Birthday").blur(function () { setTimeout(function () { var strHtml = ""; var date = new Date($("#Birthday").val().replace(/-/g, "/")); var con = getxingzuo(date.getMonth() + 1, date.getDate()); strHtml += "你的星座是:" + con; var zodiac = year[(parseInt(date.getFullYear()) + 9) % 12]; strHtml += "<br/>你的生肖是:" + zodiac; var Age = new Date().getFullYear() - date.getFullYear(); strHtml += "<br/>你的年龄是:" + Age; $("#div1").append(strHtml); }, 200); }) }) function getxingzuo(month, day) { var d = new Date(1999, month - 1, day, 0, 0, 0); var arr = []; arr.push(["魔羯座", new Date(1999, 0, 1, 0, 0, 0)]) arr.push(["水瓶座", new Date(1999, 0, 20, 0, 0, 0)]) arr.push(["双鱼座", new Date(1999, 1, 19, 0, 0, 0)]) arr.push(["牡羊座", new Date(1999, 2, 21, 0, 0, 0)]) arr.push(["金牛座", new Date(1999, 3, 21, 0, 0, 0)]) arr.push(["双子座", new Date(1999, 4, 21, 0, 0, 0)]) arr.push(["巨蟹座", new Date(1999, 5, 22, 0, 0, 0)]) arr.push(["狮子座", new Date(1999, 6, 23, 0, 0, 0)]) arr.push(["处女座", new Date(1999, 7, 23, 0, 0, 0)]) arr.push(["天秤座", new Date(1999, 8, 23, 0, 0, 0)]) arr.push(["天蝎座", new Date(1999, 9, 23, 0, 0, 0)]) arr.push(["射手座", new Date(1999, 10, 22, 0, 0, 0)]) arr.push(["魔羯座", new Date(1999, 11, 22, 0, 0, 0)]) for (var i = arr.length - 1; i >= 0; i--) { if (d >= arr[i][1]) return arr[i][0]; } } </script> </head> <body> <div id="div1" style="width:200px;height:200px;"> <input type="text" id="Birthday" value="请输入你的生日!" /> <input type="button" value="开始计算" /> </div> </body> </html>

 jQuery判断元素下是否有另一指定元素

  $(this).has("p").length > 0     //此句代码的意思是,含有P的当前元素的数量,如果含有P则为1,不含有P则为0。因为$(this)肯定是1,给它加了个条件,含有P的$(this)要是是1,要么是0 。

  另外一种方法就是用find

  $(this).find("p").length > 0  //此句代码的意思是,含有的P子元素数量是否大于0

<head>
    <script src="jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#div1").click(function () {
                if ($(this).has("p").length > 0) {
                    alert("div1有p子元素s");
                }
            })
        })
    </script>
</head>
<body>
    <div id="div1">
        <p>我是一个P</p>
    </div>
</body>

 

jQuery判断当前元素是隐藏还是显示

$(this).is(":hidden");  //如果元素是隐藏的话,则返回true

is挺好用的,他能够用jQuery选择器作为参数,特别是跟jQuery里面的选择器里面那些以冒号开头的筛选符配合使用,实现各种各样的判断。如: ":checked,:hidden"等等。给个例子:

<head>
    <script src="jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $(":button").click(function () {
                if ($(this).is(":button")) {
                    alert("我是一个按钮!");
                }
                if ($("#check1").is(":checked")) {
                    alert("我是被选中的");
                }
                if ($(".p1").is(":visible")) {
                    alert("p1是可见的");
                }
            })
        })
    </script>
</head>
<body>
    <div id="div1">
        <p class="p1">我是一个p</p>
        <input id="check1" type="checkbox" value="" />复选框
        <input type="button" value="确认" />
    </div>
</body>

 

  

posted on 2012-12-04 23:51  逆心  阅读(1870)  评论(0编辑  收藏  举报