Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 从该(选定)分区中选择子项_Javascript_Jquery_Html - Fatal编程技术网

Javascript 从该(选定)分区中选择子项

Javascript 从该(选定)分区中选择子项,javascript,jquery,html,Javascript,Jquery,Html,我有一个循环将根据需要创建div,所有div都有相同的类名, 我有功能 $(".divclassname").click(function () { var name = //I want to get the h1 text of the selected div the one the user Clicked on alert(name); }); 我尝试了很多方法,包括 $("this > h1" ).text(); $( this > "h1" ).te

我有一个循环将根据需要创建div,所有div都有相同的类名, 我有功能

$(".divclassname").click(function () {
    var name = //I want to get the h1 text of the selected div the one the user Clicked on

    alert(name);
});
我尝试了很多方法,包括

$("this > h1" ).text();
$( this > "h1" ).text();
$(this).children(h1);
$(this).children("h1").text();
我还指定了一个班,并尝试了这个

 $(this).children(".h1class").text();  
我得到的所有结果都是空的。 对不起,我是乞丐,问题可能很简单,原谅我。
非常感谢。

也许这会满足您的需要:

$(this).find("h1");

您需要委托事件处理程序来附加click事件:-

$(document).on('click', '.divclassname', function () {
 var name = $("h1", this).text();
});

并使用
$(“h1,this)
this
中查找
h1
,这样做肯定有效

$(document).on('click','.divclassname',function(){
     var name=$(this).find('h1').text();
    alert(name);
})

更改
$(“.divclassname”)。单击(function(){
使用
$(document)。在('click','.divclassname',function(){
上,单击处理程序正常,只需尝试选择第一个匹配的h1元素,如:$(this).children(“h1:first”).text()