Javascript 显示href值
我有以下代码Javascript 显示href值,javascript,php,jquery,Javascript,Php,Jquery,我有以下代码 $var = hello; echo "<a href='#".$var."' class='test'>Link</a>"; $var=hello; 回声“; 我希望当我点击链接时,href值会显示在同一页面的div中 <div> // result "#hello" showing up here </div> //结果“你好”出现在这里 我该怎么做?我应该使用POST和GET吗? 你能给我举个例子吗 谢谢首先,
$var = hello;
echo "<a href='#".$var."' class='test'>Link</a>";
$var=hello;
回声“;
我希望当我点击链接时,href值会显示在同一页面的div中
<div>
// result "#hello" showing up here
</div>
//结果“你好”出现在这里
我该怎么做?我应该使用POST和GET吗?
你能给我举个例子吗
谢谢首先,您需要为此div设置一些ID或类,使用jQuery后:
$(document).ready(function() {
$('.test').click(function() {
var attr = $('.test').attr('href');//here you take attribute and insert it into div
$('#divId').text(attr);
});
});
//这是您的href值试试看
$(".test").on('click', function() {
$("div#target").html($(this).attr('href') );
}
其中target是div的id。您可以使用jquery:
echo "<a href='#".$var."' class='test'>Link</a>";
$(document).ready(function(){
$('.test').click(function(e){//click event of link
var href = $('.test').attr('href');//get value of href
$("#my_div").html('href');//show value of href on div
return false;
});
});
echo”“;
$(文档).ready(函数(){
$('.test')。单击(函数(e){//单击链接的事件
var href=$('.test').attr('href');//获取href的值
$(“#我的div”).html('href');//在div上显示href的值
返回false;
});
});
将div的id设置为:
<div id="my_div"></div>
如果使用jQuery,可以像下面这样观看hashchange事件:
$(window).bind('hashchange', function() {
$('.mydiv').html(window.location.hash);
});
<div class="mydiv"></div>
$(window.bind('hashchange',function()){
$('.mydiv').html(window.location.hash);
});
显示呈现的html。?是否可以像这样在div中回显“attr”var?是的,可以。但是你为什么要这么做呢?它就像一辆购物车。其中有5个选项作为链接,当您单击它时,href值显示在div中。但我想知道是否可以将attr var从javascript传递到php。例如,要在php中连接另一个值或在其他函数中使用它,您可以在其他函数中使用它,您可以将其设置到会话中,但您需要刷新页面以从会话中获取值,或者您可以使用ajax?并将值发送到php文件而不刷新page@sergio:在运行模仿程序后,现在非常重要的是不要回答手动材料问题。这些都是最简单、最直接的答案,因为它更适合图书馆或langauge的文档。否则你只会小心地喂吸血鬼,这实际上是在贬低这个网站的实用性。谢谢您的合作。