如何知道锚在文本或图像上

How to know that the anchor was on text or image?

本文关键字:图像 文本 何知道      更新时间:2023-09-26

在我的问题中,我遇到了获取锚文本并将其打印在重定向屏幕上的问题:如何获取Session变量中的锚文本?我从这个问题中得到了解决方案,但现在如果锚在图像上,我如何传递一些文本以将其打印在重定向页面上?有人请在链接中查看上面的问题并帮助我。提前感谢

您的问题是<a><img src="sample.jpg"></a>正确的。使用window.location.href = $(this).attr("href") + "&title=" + $(this).text();传递某些标题,并将标题打印为

var a = window.location.href.split('&')  
var print = a[a.length - 1]
console.log(print)

希望这将对您有所帮助

您可以使用data-属性:

<a class="customLink" href="your/url" data-text="yourText"><img src=".." alt=".." /></a>

并发送带有来自data-text属性的文本的url:

$(document).on('click', '.customLink', function(e) {
   e.preventDefault();
   var that = $(this);
   var url = that.attr('href');
   var text = that.attr('data-text');
   // Continue here ...
})