如果声明没有触发警报框

Rai*_*ner -2 javascript jquery

我正在尝试创建一个if语句,如果div中有文本,则触发警告框.

我的Jsfiddle:http://jsfiddle.net/D7cPT/21/

我的HTML:

<div id="post-preview">aasasdasd</div>
Run Code Online (Sandbox Code Playgroud)

我的观察:

$(document).ready(function() {
    // Bind it to some action.
    if{$('#post_body_html').html();) {
       alert('asdasda');
      }
});
Run Code Online (Sandbox Code Playgroud)

Cha*_*had 13

哦亲爱的神:

//why is there a bracket after the if and a semicolon in there?
if{$('#post_body_html').html();) { 
Run Code Online (Sandbox Code Playgroud)

怎么样:

//change if{ to if( and remove semicolon
if($('#post_body_html').html()) {
Run Code Online (Sandbox Code Playgroud)

您的选择器也与元素的ID不匹配.更改#post_body_html#post-preview

的jsfiddle

编辑:

对于那些以后登陆的人,写一个更好的方法来完成相同的测试:

if($('#post_body_html')[0].childNodes.length) {
Run Code Online (Sandbox Code Playgroud)

代替.