我不太熟悉 JavaScript/JQuery,我正在尝试使特定的 WordPress 插件以这种方式工作:
当用户单击的 URL 包含子字符串 securepdf/
时,必须触发该事件在里面。此类 URL 的示例为:http://igcse.net/wp-content/uploads/securepdfs/2017/10/sample-phy.pdf
我只想添加if
代码中的声明。这是代码:
jQuery(document).ready(function($){
// Custom jQuery goes here
$('[data-pdf]').click(function(event) {
$('.pdfembedder_item').hide();
var id = $(this).data('pdf');
$('#'+id).show();
jQuery('.pdfemb-viewer').pdfEmbedder();
});
});
据我所知,这段代码通过调用pdfEmbedder()
来呈现pdf。当用户点击 URL 时起作用。如果您需要更多详细信息,请告诉我。
最佳答案
您可以使用“indexOf”查看网址,如果“/securepdf/”在网址上,它将返回索引,否则返回-1。
var url = document.location.pathname;
if (url.indexOf('/securepdf/') !== -1) {
// there is securepdf on the url
} else {
// there isn't securepdf on the url
}
请记住,如果您仅搜索“securepdf”,不带斜杠,您可能会得到一些在路径中散布有 securepdf 的网址(例如“http://(...)/notasecurepdf.html ”),而且不仅符合您的条件想要。
关于javascript - 添加 if 子句,检查 url 中是否包含 'securepdf',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897264/