简体   繁体   English

jquery/ajax 加载 href onClick 获取多个链接

[英]jquery/ajax loading href onClick for multiple links

Building a menu inside of ionic 4 progressive web application and I am using ajax /jquery to load pages into a div .在 ionic 4 渐进式 Web 应用程序中构建菜单,我使用 ajax /jquery 将页面加载到div Is there any way to tell the jquery / to load the href src= of the clicked element with the specific class.有什么方法可以告诉 jquery / 加载具有特定类的单击元素的href src= instead of adding this same code 20 times to load each different page而不是添加相同的代码 20 次来加载每个不同的页面

$('.classofButton').click ( function () {
     $('#content').load ('href of clicked object or link etc') ; 
} );

You can get the href by using this您可以使用this获取href

$('.classofButton').click ( function () {
     $('#content').load ($(this).attr('href')) ; 
} );

You can do it like this.你可以这样做。

 $('.classofButton').click(function(event) { event.preventDefault(); var href = $(this).attr('href'); $('#content').load(href) ; });
 #content { width: 200px; height: 200px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a class="classofButton" href="http://google.com">google.com</a> <a class="classofButton" href="https://yahoo.com">yahoo.com</a> <div id="content"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM