You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何$(this).siblings('.bidbutton')返回undefined?点击弹窗数字失效

问题分析与解决

错误核心

问题出在混淆了jQuery对象与原生DOM元素的属性调用方式:
$(this).siblings('.bidbutton') 返回的是jQuery包装后的对象,而非原生DOM节点,直接访问innerHTML属性会返回undefined。

修正方案

方案1:用jQuery方法获取文本(推荐)

jQuery提供了.text()方法专门用于提取元素文本内容,写法简洁且适配jQuery对象:

$(".fa-plus-circle").click(function(){
    alert($(this).siblings('.bidbutton').text());
});

方案2:转为原生DOM元素后使用innerHTML

如果习惯原生DOM操作,可以通过索引[0]或.get(0)将jQuery对象转为原生元素,再调用innerHTML:

$(".fa-plus-circle").click(function(){
    alert($(this).siblings('.bidbutton')[0].innerHTML);
    // 等价写法:alert($(this).siblings('.bidbutton').get(0).innerHTML);
});

方案3:鲁棒性更强的层级查找(可选)

若后续HTML结构可能调整,推荐先定位父容器再查找目标元素,避免层级变动导致失效:

$(".fa-plus-circle").click(function(){
    alert($(this).closest('.antal').find('.bidbutton').text());
});

效果验证

以上三种方案均能在点击.fa-plus-circle时,正确弹窗显示对应.bidbutton内的数字(55或45)。

内容的提问来源于stack exchange,提问作者user19758032

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:13:17