Javascript onClick根据单击的内容设置href值
当我将attribute设置为a href时,我试图找到一种显示PHP的方法。 这就是我目前所拥有的Javascript onClick根据单击的内容设置href值,javascript,php,Javascript,Php,当我将attribute设置为a href时,我试图找到一种显示PHP的方法。 这就是我目前所拥有的 window.onload = function () { document.getElementById('drainage').onclick = function () { document.getElementById('link').setAttribute( 'href', 'Drainage/' <?php echo $conn["area"]; ?>
window.onload = function () {
document.getElementById('drainage').onclick = function () {
document.getElementById('link').setAttribute( 'href', 'Drainage/' <?php echo $conn["area"]; ?> );
};
document.getElementById('electrics').onclick = function () {
document.getElementById('link').setAttribute( 'href', 'Electrics/' );};}
window.onload=函数(){
document.getElementById('drawing')。onclick=function(){
document.getElementById('link').setAttribute('href','drawing/');
};
document.getElementById('electrics')。onclick=function(){
document.getElementById('link').setAttribute('href','Electrics/');};}
地区=英国的不同城镇
所以我的url应该显示为www.example.co.uk/drawing/Bristol
到目前为止,它只显示为排水或排水/未定义不确定这是否是实现我想要的最好的方式,但我已经尝试了一段时间,并决心使它工作
编辑
排水和电气是模式链接,单击时显示从数据库中提取的城镇列表
示例
如果单击排水系统,则城镇将加载,用户可以从列表中选择城镇,然后该城镇将路由到该城镇页面,但URL应在单击排水系统链接时设置为排水系统/dynamictown。我不确定函数
setAttribute
是否存在,但您可以尝试以下操作:
window.onload = function() {
document.getElementById('drainage').onclick = function() {
document.getElementById("link").href="YOUR_NEW_LINK";
}
//Do that for the second button aswell
}
我不确定函数
setAttribute
是否存在,但您可以尝试以下方法:
window.onload = function() {
document.getElementById('drainage').onclick = function() {
document.getElementById("link").href="YOUR_NEW_LINK";
}
//Do that for the second button aswell
}
首先是语法错误:
document.getElementById('link').setAttribute( 'href', 'Drainage/' <?php echo $conn["area"]; ?> );
document.getElementById('link').setAttribute('href','drawing/');
应该是:
document.getElementById('link').setAttribute( 'href', 'Drainage/<?php echo $conn["area"]; ?>');
document.getElementById('link').setAttribute('href','drawing/');
第二,如果在页面加载上加载区域($conn[“area”]),则可以实现这一点。
加载页面时,$conn变量中应有区域
像这样:
<a id="link">Test</a>
<button id="drainage">click</button>
<button id="electrics">click</button>
<?php
$conn["area"] = "Bristol"; // comment this line when you have dynamically
?>
<script>
window.onload = function () {
document.getElementById('drainage').onclick = function () {
document.getElementById('link').setAttribute( 'href', 'Drainage/<?php echo $conn["area"]; ?>');
};
document.getElementById('electrics').onclick = function () {
document.getElementById('link').setAttribute( 'href', 'Electrics/' );
};
}
</script>
测试
点击
点击
window.onload=函数(){
document.getElementById('drawing')。onclick=function(){
document.getElementById('link').setAttribute('href','drawing/');
};
document.getElementById('electrics')。onclick=function(){
document.getElementById('link').setAttribute('href','Electrics/');
};
}
第一件事是语法错误:
document.getElementById('link').setAttribute( 'href', 'Drainage/' <?php echo $conn["area"]; ?> );
document.getElementById('link').setAttribute('href','drawing/');
应该是:
document.getElementById('link').setAttribute( 'href', 'Drainage/<?php echo $conn["area"]; ?>');
document.getElementById('link').setAttribute('href','drawing/');
第二,如果在页面加载上加载区域($conn[“area”]),则可以实现这一点。
加载页面时,$conn变量中应有区域
像这样:
<a id="link">Test</a>
<button id="drainage">click</button>
<button id="electrics">click</button>
<?php
$conn["area"] = "Bristol"; // comment this line when you have dynamically
?>
<script>
window.onload = function () {
document.getElementById('drainage').onclick = function () {
document.getElementById('link').setAttribute( 'href', 'Drainage/<?php echo $conn["area"]; ?>');
};
document.getElementById('electrics').onclick = function () {
document.getElementById('link').setAttribute( 'href', 'Electrics/' );
};
}
</script>
测试
点击
点击
window.onload=函数(){
document.getElementById('drawing')。onclick=function(){
document.getElementById('link').setAttribute('href','drawing/');
};
document.getElementById('electrics')。onclick=function(){
document.getElementById('link').setAttribute('href','Electrics/');
};
}
这是一个“php”问题吗?因为我问的是如何将php字符串输出到我的函数中?这是一个“php”问题吗?因为我问的是如何将php字符串输出到我的函数中?可以工作,但$conn[“area”]总是不同的,所以我们不能有一个静态值。我只是告诉您,在加载页面时需要有该值。您不需要在变量上分配静态数据,但请确保在该页上的$conn变量中有区域[“区域”]在$conn变量中。可以工作,但$conn[“区域”]将始终不同,因此我们不能有静态值。我只是告诉您,在加载页面时需要有该值。您不需要在变量上分配静态数据,但请确保在该页上的$conn变量中有$conn[“area”]区域。