javascript - 更改 ID 内的类

标签 javascript jquery

这是类名为“assistance_wrap”的示例 div。

<div class="assistance_wrap">
<div class="top"></div>
<div class="header highlight">Types of assistance offered at this event:</div>
<div class="nearby_header highlight">This event is currently full, but there are other ways you can</div>
</div>

我已经克隆了这个 div,每个克隆版本都附加了一个唯一的 ID。

<div id="assistance_wrap_315" class="assistance_wrap">
<div class="top"></div>
<div class="header highlight">Types of assistance offered at this event:</div>
<div class="nearby_header highlight">This event is currently full, but there are other ways you can</div>

<div id="assistance_wrap_316" class="assistance_wrap">
<div class="top"></div>
<div class="header highlight">Types of assistance offered at this event:</div>
<div class="nearby_header highlight">This event is currently full, but there are other ways you can</div>

在我的 JS 中,我将名称 new_info 分配给类为“assistance_wrap”的示例 div 的副本 并为其分配唯一的 ID 并显示在我的页面上。 现在我想更改类 nearby_header 突出显示 中的文本,谁的父 div id 是 assistance_wrap_316我应该怎么做?

这是我尝试过的。日志的输出基本上是id为assistance_wrap_316的div的整个文本。

console.log(new_info.find('#assistance_wrap_'+ event_data.eventId , '.nearby_header highlight').text());

假设event_data.eventId的值为316

最佳答案

您应该删除",":

console.log($('#assistance_wrap_'+ event_data.eventId + ' .nearby_header').text());

或使用find()方法:

console.log($('#assistance_wrap_'+ event_data.eventId).find('.nearby_header').text());

关于javascript - 更改 ID 内的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11531873/

相关文章:

c# - 尝试将对象转换为 javascript []{}- C# 键值集合或字典<string,object> 或<string, string>

PHP、AJAX、HTML、MySQL,单击按钮从数据库中获取信息

php - jquery $.get() 返回 [object,object]

javascript - 为什么我的 event.currentTarget 会自动更改?

javascript - jquery/javascript 函数只能工作一次

javascript - MomentJs 获得年 - 没有区别

javascript - 如何将其添加到函数中以回调 handleChange 和 componentDidMount?

javascript - FlowType:类型的继承(类型A是类型B的子集……)

javascript - bxSlider - 在 slider 内移动控件

php - 使用 Ajax 动态加载内容