Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何查找包含h1的td元素,然后向td添加onclick事件_Javascript_Jquery_Html_Onclick - Fatal编程技术网

Javascript 如何查找包含h1的td元素,然后向td添加onclick事件

Javascript 如何查找包含h1的td元素,然后向td添加onclick事件,javascript,jquery,html,onclick,Javascript,Jquery,Html,Onclick,我需要在Jquery中添加一个onclick事件。这是标准-如果td包含h1元素,则需要将其添加到h1元素之后的下一个td中。例如,我有以下html: <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td colspan="2"><h1>Activity Packs</h1></td> </tr> <

我需要在Jquery中添加一个onclick事件。这是标准-如果td包含h1元素,则需要将其添加到h1元素之后的下一个td中。例如,我有以下html:

<table cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td colspan="2"><h1>Activity Packs</h1></td>
    </tr>
    <tr>
        <td colspan="2"><P>Test test Test test Test testTest test  Test test Test test Test test Test test Test test Test test Test test Test test Test test Test test Test test. Link: <a href="/products">I am a link</a></p></td>
    </tr>

活动包

测试测试测试测试测试测试。链接:

onclick事件需要添加到任何“a”链接,而不是h1标题后的td(包含所有文本的td)


我无法控制html,因此无法添加任何id或任何内容。

请尝试以下操作:

$('table h1').closest('tr').next().find('a').on('click', function(e) {
    e.preventDefault();
    alert($(this).text());
});
演示:

或更有效:

$('table h1').closest('tr').next().on('click', 'a', function(e) {
    ...
});
请试试这个

$('table tr td').each(function(){
   if($(this).find('h1')[0] != undefined){
     $($(this).find('h1')[0]).after('<a href="#">abc</a>');
   }
})
$('table tr td')。每个(函数(){
if($(this.find('h1')[0]!=未定义){
$($(this.find('h1')[0])。在('')之后;
}
})
试试这个:

$(“td”)。每个(函数(){ 如果($(this).has(“h1”){ $(this.next().append(“click”); }
})

您能写出在示例HTML中可以找到的内容以及它将被替换为什么内容吗?