这里的任务很简单,但我无法做到正确:如果单击一个表格单元格,我需要检查它是否包含input字段.如果它不存在,则应创建一个新的.
到目前为止我得到了这个:
$("tbody td").bind("click", function(){
$this = $(this);
var newInput = $(document.createElement("input")).attr("class", "input-small");
$this.append(newInput);
})
Run Code Online (Sandbox Code Playgroud)
这是有效的,但正如您所看到的,如果输入已存在,则会错过测试.我已经尝试了各种方法if($this.text.length){...},if($this.val().hasClass("input-small") == true){...}但都失败了.那我该怎么做呢?什么是正确的方法来检查点击的单元格是否包含输入字段?
Rus*_*Cam 10
像下面这样的东西会起作用
if ($this.find('input').length) {
// the td clicked contains an <input>
}
Run Code Online (Sandbox Code Playgroud)
$this是一个包装当前<td>元素(引用this)的jQuery对象,因此我们需要在DOM中查看此元素以查看它是否包含<input>元素.如果是,则.length属性将大于0,从而达到真值.
| 归档时间: |
|
| 查看次数: |
7742 次 |
| 最近记录: |