Javascript 如何获取警报框中的按钮链接
在下面的代码中,我尝试获取按钮href属性值,并使用jquery在警报框中显示它 当我因为href值(即$value['3'])而单击按钮时,它将重定向到我们的零售商链接,但通过使用jquery,当我通过使用href属性获得没有页面刷新的按钮链接时,它只获取任何按钮链接上的第一个链接。 事实上,我不想在没有页面刷新的情况下使用特定的按钮链接Javascript 如何获取警报框中的按钮链接,javascript,php,jquery,html,Javascript,Php,Jquery,Html,在下面的代码中,我尝试获取按钮href属性值,并使用jquery在警报框中显示它 当我因为href值(即$value['3'])而单击按钮时,它将重定向到我们的零售商链接,但通过使用jquery,当我通过使用href属性获得没有页面刷新的按钮链接时,它只获取任何按钮链接上的第一个链接。 事实上,我不想在没有页面刷新的情况下使用特定的按钮链接 <html><head> <script src="jquery-1.11.3.js"></script>
<html><head>
<script src="jquery-1.11.3.js"></script></head>
<script>
$(document).ready(function(){
$('.button').click(function(){
var href = $('a').attr('href');
alert(href);
})
});
</script><body>
<?php
$data=array(
array("HTC desire 210 black","Flipkart",20000,"http://www.flipkart.com"),
array("HTC desire 326 white","Snapdeal",22000,"http://www.snapdeal.com"),
array("HTC desire 516","Amazon",23000,"http://www.amazon.in")
);
foreach($data as $value)
{
?>
<a href="<?php echo $value['3']; ?>"><input type="button" value="Get link's href value" class="button" /></a><br/><br/>
<?php
}
?></body></html>
$(文档).ready(函数(){
$('.button')。单击(函数(){
var href=$('a').attr('href');
警报(href);
})
});
那么您想在单击链接时提醒链接的href值吗?使用preventDefault()
或者如果你只想点击一个按钮
$(document).ready(function(){
$('.button').click(function(e){
alert( $(this).parent('a').attr('href') );
e.preventDefault();
return false;
})
});
尝试:
试试这个,这对我来说很好
$(文档).ready(函数(){
$('.button')。单击(函数(){
var href=$(this.nexist('a').attr('href');
警报(href);
});
});代码>
脚本中的更改
<script>
$(document).ready(function(){
$(document).on('click','.button',function(){
var href = $(this).attr('data-link');
alert(href);
})
});
</script>
$(文档).ready(函数(){
$(文档)。在('单击','按钮',函数()上){
var href=$(this.attr('data-link');
警报(href);
})
});
html中的更改
<input type="button" value="Get link's href value" class="button" data-link="<?php echo $value['3']; ?>"/><br/><br/>
Ya如果我可以使用elsewheretry localstoragelocalstorage.setItem('href',href'),那么我如何在会话变量中存储上述“href”变量呢代码>确定然后如何在其他页面回显此会话变量以进行测试。$('body').append(localStorage.getItem('href'))代码>@shopeeon很乐意帮忙。别忘了投票/接受答案。@shopeeon这是你真正想要的吗?
<script>
$(document).ready(function(){
$(document).on('click','.button',function(){
var href = $(this).attr('data-link');
alert(href);
})
});
</script>
<input type="button" value="Get link's href value" class="button" data-link="<?php echo $value['3']; ?>"/><br/><br/>