jquery substr 未定义错误

jquery substr undefined error

所以我想对所选属性进行子字符串化,但出现错误: "TypeError: src is undefined"

代码如下:

js

function reset_select(){
 var src = $('td.onedit').prop('id');
 var new_stuhl = src.substr(1, 1);
 ...
}

Html:

<tr>    
<td id="s1.1" class="names onedit"><p class="name">Name Name</p></td>
</tr>

console.log(src) 给我字符串 's1.1' 和 typeOf 是 'string'。当我改变

var src = "s1.1";

有效。我正在使用 jQuery 3.2.1.

为什么这不起作用?选择器有问题吗?

required 在你的 <tr> 标签周围有一个 <table> 标签。

@SandeepNayak, you can read more about why in this question: How do browsers analyze <tr> <td> without <table>?

所述

它工作正常:

function reset_select(){
     var src = $('td.onedit').prop('id');
     var new_stuhl = src.substr(1, 1);
     
     console.log(src);
     console.log(new_stuhl);
}

reset_select();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>    
        <td id="s1.1" class="names onedit"><p class="name">Name Name</p></td>
    </tr>
</table>