Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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/2/jquery/83.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 获取按钮所在div的div内的文本_Javascript_Jquery_Html - Fatal编程技术网

Javascript 获取按钮所在div的div内的文本

Javascript 获取按钮所在div的div内的文本,javascript,jquery,html,Javascript,Jquery,Html,基本上,我有一些按钮 <div class="button"> <!--Some other stuff--> <div class="id">HD5sjW</div> </div> <div class="button"> <!--Some other stuff--> <div class="id">yqWH3X</div> </div> <div

基本上,我有一些按钮

<div class="button">
  <!--Some other stuff-->
  <div class="id">HD5sjW</div>
</div>

<div class="button">
  <!--Some other stuff-->
  <div class="id">yqWH3X</div>
</div>

<div class="button">
  <!--Some other stuff-->
  <div class="id">KWZy5V</div>
</div>
确认只是为了一个明显的输出


另外,我应该如何更好地组织问题和标题类型?

您可以在jQuery函数中使用或使用上下文参数

$('.button').hover(function () {
        confirm("http://domain.com/thing?id=" + $(".id", this).text());
});

$('.button').hover(function () {
        confirm("http://domain.com/thing?id=" + $(this).find('.id').text());
});

这里有一个使用
的解决方案。使用
单击()
。悬停()
将导致确认框多次触发

$('.button')。单击(函数(){
//找出这是哪个巴特顿
var currentBtn=$('.button').index($(this));
//使用currentBtn使用“:eq()查找其匹配的id div`
var currentId=$('.id:eq('+currentBtn+')).text();
确认(”http://domain.com/thing?id=“+当前ID)
});

HD5sjW

yqWH3X
KWZy5V
这比我的干净。我会提醒你使用。但要悬停。查看此小提琴-->可以看到,使用.hover时,确认框会被多次触发。为什么不使用real
元素?如果没有
选项卡索引
角色
等,这些“按钮”将无法访问。
$('.button').hover(function () {
        confirm("http://domain.com/thing?id=" + $(".id", this).text());
});

$('.button').hover(function () {
        confirm("http://domain.com/thing?id=" + $(this).find('.id').text());
});