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

如何使用jQuery从指定类中获取soundManager的soundId

使用jQuery获取soundManager bar-ui组件的soundId

嘿,我来帮你搞定这个问题~观察你给出的HTML结构,能看到播放器的容器ID是sm2-99943,而soundManager的bar-ui组件有个默认规则:容器ID里sm2-后面的数字就是对应的soundId。

下面给你几种实用的jQuery实现方式:

方法一:针对单个播放器提取

如果只需要获取指定播放器的soundId,可以直接从容器ID中截取:

// 选中目标播放器容器
const $player = $('#sm2-99943');
// 从ID中移除前缀"sm2-",得到soundId
const soundId = $player.attr('id').replace('sm2-', '');
console.log(soundId); // 输出结果:99943

方法二:批量获取多个播放器的soundId

如果页面上有多个bar-ui播放器,想要批量获取它们的soundId,可以遍历所有播放器元素:

// 遍历所有带sm2-bar-ui类的播放器
$('.sm2-bar-ui').each(function() {
  const currentSoundId = $(this).attr('id').replace('sm2-', '');
  console.log('当前播放器的soundId:', currentSoundId);
});

额外补充:关联soundManager实例

如果之后需要操作对应的sound对象,还可以通过拿到的soundId直接调用soundManager的API:

// 通过soundId获取对应的sound实例
const soundInstance = soundManager.getSoundById(soundId);
// 也可以反过来,如果已有sound实例,直接获取它的ID
// const soundId = soundInstance.id;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:24:26