jQuery 在其中选择 p

ini*_*ium -2 html jquery jquery-ui jquery-selectors

我在选择单击的 ID 内的 ap 标签时遇到一些问题,代码如下;

<tbody>
    <tr id="test1">
    <p>
        Some random text that should show up in the dialog.
    </p>
        <td>test</td>
        <td>test</td>
    </tr>
    ... the rest of the tr's are identical, nothing else to see here.
 </tbody>


$('#test1, #test2, #test3, #test4').click(function(){
    $(this 'p').dialog();
});
Run Code Online (Sandbox Code Playgroud)

现场测试;http://team-blandsaft.no-ip.org/

最好习惯在 stackoverflow 编辑器中编写一些代码。

ᾠῗᵲ*_*ᵲᄐᶌ 5

使用.find()

$(this).find('p').dialog();
Run Code Online (Sandbox Code Playgroud)

或者您可以使用上下文选择器

$('p',this).dialog(); 
Run Code Online (Sandbox Code Playgroud)

它内部使用 find 方法

正如其他人提到的,您的 html 无效,作为<p>其子项<tr>

来自tr 的MDN 文档

允许的内容:零个或多个<td><th>元素,或它们的混合

  • 它*仍然*不会工作,因为当构建 DOM 时,`p` 将被移动到 `table` 元素之外,*因为它是无效的 html*: [JS Fiddle](http://jsfiddle.网/davidThomas/UQ2NL/)。 (2认同)