【问题标题】:how can send data-* attr to a js function?如何将 data-* attr 发送到 js 函数?
【发布时间】:2019-10-31 21:20:12
【问题描述】:

我需要在我的joomla网站中使用jplayer添加到播放列表,当用户点击播放图标时,jquery会从标签链接中获取data-mp3和data-name和data-poster并添加到上面的函数中添加播放器中的声音 请帮助我是怎么做到的? 我无法获取 data-* 属性并发送到 myplaylist.add 函数以将该声音数据添加到 jplayer 的播放列表中

$("#playlist-equivalent-1-a"). click(function() {
    myPlaylist.add({
        title:"title",
        artist:"artist",
        mp3:"url",
        poster: "url"
    }, true);
});

【问题讨论】:

标签: javascript jquery jplayer


【解决方案1】:

你很幸运,jQuery 有一个用于检索data-* 属性的内置函数: https://api.jquery.com/data/.

示例:从锚标签中获取 mp3 标题和艺术家

$('#datatag').click(function() {
  $('#output').text($('#datatag').data('title') + ' from ' + $('#datatag').data('artist') + ' is a really good song!');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a data-artist="De Jeugd van Tegenwoordig" data-title="Sterrenstof" id="datatag">Click me to reveal my data.</a><br>
<span id="output"></span>

【讨论】:

  • 这不是一个正确的答案。将您的代码实施到 OP 示例。 OP 知道data-*
  • 我需要将该 attr 发送到 myPlaylist.add 并在其后运行此函数,例如 myPlaylist.add({ title:"Sterrenstof", artist:"De Jeugd van Tegenwoordig", mp3:"your url ", 海报: "你的网址" }, true);并在设置此属性后运行此功能
  • @mohammadjavadjabbari 你能再澄清一下吗?如果我猜对了,您希望: 1. 从某个元素获取数据属性 2. 将它们传递给 myPlaylist.add 3. 运行一个函数(哪个?)
  • @Fdebijl 首先我需要从某个元素获取数据属性,然后将它们传递给 myPlaylist.add 然后运行 ​​myPlaylist.add 函数,因为此函数将该音乐数据添加到我的 jplayer 播放列表中——带有播放列表演示的 jplayer :jplayer.org/latest/demo-02-jPlayerPlaylist -- 一个和我的题目类似的项目:zakerin.ir
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多