检查所选链接在多个给定链接中是否匹配
Check if the selected link match in multiple given links
我有以下播放列表行,这些行已经存在于播放列表中
<div class="total-row">
<div class="total-title" src="music/04_LCR_Mae_Terra.mp3">04_LCR_Mae_Terra</div>
</div>
<div class="total-row">
<div class="total-title" src="music/06LCR_Homem_Mal.mp3">06LCR_Homem_Mal</div>
</div>
<div class="total-row">
<div class="total-title" src="music/06LCR_Homem_Mal.mp3">06LCR_Homem_Mal</div>
</div>
而且我有链接可以在播放列表中添加歌曲
<div id="playlinks">
<li mp3="music/04_LCR_Mae_Terra.mp3" class="add_link"></li>
<li mp3="music/05_LCR_Jack_Matador.mp3" class="add_link"></li>
</div>
所以我会将所选(点击)链接的mp3链接与播放列表中已经存在的链接进行匹配(我会与所有行进行匹配),如果已经存在,则不添加,如果不存在,则添加所选链接。我正在使用以下jquery代码
if i use the below code
$(document).on("click", ".add_link", function(){
var mp3 = $(this).attr("mp3");
var foundInPlaylist =
$(".total-row .total-title[src$='"+ ('/'+mp3) +"']").length;
if(foundInPlaylist){
alert("found");
} else{
alert("not found");
}
});
这不起作用,即使我只是写警报(foundInPlaylist);它提醒"0";
if i use the second solution
$(".add_link").click(function () {
var newMp3 = $(this).attr("mp3");
var exists = false;
$(".total-row").each(function(){
var oldmp3 = $(this).children(".total-title").attr("src");
if(oldmp3.indexOf(newMp3) > 0 )
{
exists = true
}
});
if(exists)
{
alert("song already exists");
}
else {
alert("song does not exist");
}
});
它也不适用于我
感谢
也许您可以尝试使用jQuery"attribute ends with"选择器$("[attribute$='value']")
:
http://api.jquery.com/attribute-ends-with-selector/
小提琴中的快速示例:http://jsfiddle.net/KRnWR/2/
// http://api.jquery.com/on/
$(document).on("click", ".add_link", function(){
var mp3 = $(this).attr("mp3");
// http://api.jquery.com/attribute-ends-with-selector/
var foundInPlaylist =
$(".total-row .total-title[src$='"+ ('/'+mp3) +"']").length;
// for (brain)killing programmers :)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
if( foundInPlaylist ){
alert( mp3 + ' found in playlist.');
}else{
alert( mp3 + ' not found in playlist.');
}
});
我在mp3的文件名之前准备了'/'
,以防止出现以下错误匹配:
mp3="lala.mp3">
src="music/lalalala.mp3">
请尝试此代码:
$(".add_link").click(function () {
var newMp3 = $(this).attr("mp3");
var exists = false;
$(".total-row").each(function(){
var oldmp3 = $(this).children(".total-title").attr("src");
if(oldmp3.indexOf(newMp3) > -1 )
{
exists = true
}
});
if(exists)
{
alert("song already exists");
}
else {
alert("song does not exist");
}
});
这是一个工作示例:
http://jsfiddle.net/YW46k/
相关文章:
- 如何检查链接是否有文本,并根据文本值评估条件-Jquery/JS
- 是否可以检测链接是否已被JavaScript禁用
- 链接是否可以调用它指向的页面上存在的 javascript
- 检查链接是否为图像
- 这种类型的 img 链接是否有插件
- ios:如何检测自定义url方案链接是否失败
- 在不下载文件的情况下检查下载链接是否正常工作
- Javascript-根据链接是否属于图像来分离链接
- 检测HTML链接是否被点击
- 在onclick上确定所单击的链接是否意味着打开一个新窗口或选项卡
- 检查链接是否已在另一个窗口中打开[JS或jQuery]
- 我可以确定单击链接是否成功打开了一个新窗口/选项卡
- 如何检测链接是否被点击时的窗口.Onbeforeunload被触发
- 使用jQuery检查链接是否存在
- 如何使用javascript检查某个网站的链接是否正确
- 如何在javascript中检查链接是否被点击
- 我们可以检测一个链接是否在一个有jQuery.click()的DIV中被点击了吗
- 按钮和链接是否会增加移动响应度
- 检测链接是否用于访问页面
- 检查链接是否包含javascript