[英]trying to filter a link using jquery. dont know how to put filter() into use here
i want to just get 'whatsapp'我只想得到'whatsapp'
https://api.whatsapp.com/send?text=" +
"ploads/2022/08/Offer-Image-Intro-647-x-742-E.jpg"e=
can someone help?有人可以帮忙吗?
I know the concept of split() and filter(), but don't know to use the to only select whatsapp我知道 split() 和 filter() 的概念,但不知道只使用 select whatsapp
this is my piecd of code:这是我的一段代码:
jQuery('div.share_block > div > a').click(function(){
var _shareLink =jQuery(this).attr('href').filter() === "whatsapp";
var visit_link= jQuery(this).prevUntil('div.offer_hover_block > a').text();
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'whatsapp_click',
'carouselHref' : _shareLink,
'pageName' : visit_link
});
});
EDITED: I just want to filter out whatsapp.编辑:我只想过滤掉whatsapp。 my code above has a var shareLink which prints out the whole whatsapp link.
我上面的代码有一个 var shareLink 打印出整个 whatsapp 链接。 i want to add a function to just filter out whatsapp
我想添加一个 function 来过滤掉whatsapp
Based on your code, it appears you are trying to test if whatsapp
string is part of the URL.根据您的代码,您似乎正在尝试测试
whatsapp
字符串是否是 URL 的一部分。 Consider the following.考虑以下。
jQuery('div.share_block > div > a').click(function(){
var _shareLink = (jQuery(this).attr('href').indexOf("whatsapp") > -1);
var visit_link = jQuery(this).prevUntil('div.offer_hover_block > a').text();
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'whatsapp_click',
'carouselHref' : _shareLink,
'pageName' : visit_link
});
});
The
indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.indexOf()
方法返回可以在数组中找到给定元素的第一个索引,如果不存在则返回 -1。
See More: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf查看更多: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
If you have a URL like:如果你有一个 URL 像:
https://api.whatsapp.com/send?text=ploads/2022/08/Offer-Image-Intro-647-x-742-E.jpg"e=
And an expression like:像这样的表达式:
var _shareLink = jQuery(this).attr('href').indexOf("whatsapp");
The value of _shareLink
will be: _shareLink
的值将是:
12
You can then use this as a logical statement to get true
or false
.然后,您可以将其用作逻辑语句来获取
true
或false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.