字符串比较在Chrome中无效

时间:2012-03-26 06:48:14

标签: javascript jquery google-chrome safari opera

这似乎很奇怪,但它确实发生了。 我有这个代码在IE8和FF中工作,但我感到震惊的是它并没有真正在谷歌Chrome中工作

/*btnPost*/
$("input#btnPost").click(function(){
    //check if the textbox contains anything...
    var txtPostdata = $.trim($("div#txtPost").html());
    if(txtPostdata.length <= 0){
        d = createDialog("confirmation","are you sure to post a blank post?",{"modal":true});
        d.dialog("open");
    }
});   

那些不起作用的东西是chrome无法知道 txtPostdata 变量的长度。 txtPostdata 包含在 CONTENTEDITABLE DIV 中获取的html。 我试图在if语句中拔出TODO,它工作正常。我也尝试过这样做

/*btnPost*/
$("input#btnPost").click(function(){
    //check if the textbox contains anything...
    var txtPostdata = $.trim($("div#txtPost").html());
    if(txtPostdata == ""){
        d = createDialog("confirmation","are you sure to post a blank post?",{"modal":true});
        d.dialog("open");
    }
});

但仍然无法正常工作......有什么方法可以解决这个问题吗?我的chrome版本是17.0。

请试一试 here

2 个答案:

答案 0 :(得分:1)

当我使用.text()代替.html()时,它工作正常。

var txtPostdata = $.trim($("#txtPost").text());

答案 1 :(得分:0)

if(txtPostdata==undefined){
        d = createDialog("confirmation","are you sure to post a blank post?",{"modal":true});
        d.dialog("open");
    }
相关问题