我只希望用户单击一个名称,然后弹出(提示)一些信息。在标签中,如果我不传递任何参数,它的确会发出警报,否则即使我放置了类似的内容也不会发出任何警报
<a href='javascript:sayThis('hi');'>
这是我的html代码(由某些PHP生成):
PHP:
function showThis($color, $withFunctionYesOrNo, $first_levelIdFunction, $first_levelNameFunction, $first_levelLastNameFunction, $agent_email){
if($withFunctionYesOrNo == 'yes'){
$showThis = "<br>
<div>
<div style='margin-left:5%; border-left:6px solid ".$color."'>------
<a href='javascript:sayThis('".$agent_email."');'>
".$first_levelLastNameFunction.", ".$first_levelNameFunction." (ID: ".$first_levelIdFunction.")
</a>
<input type='submit' class='more' id='more' value='+' onclick='agentsBelow(".$first_levelIdFunction.")'>
</div>
<div style='margin-left:7%;' id=".$first_levelIdFunction."></div>
</div>";
}
return $showThis;
}
这是我的JS代码:
function sayThis(email){
alert(email);
}
首先,我想说我永远不会使用您的编码实践。您不仅在各处都有报价问题,而且onclick
Event Object传递给Event Handler agentsBelow()
。在这些情况下,我将这样编写代码:
document.getElementById('more').onclick = function(e){
e = e || event; // don't even need to use the `e` argument above or this if not using the Event Object
agentsBelow('whatever');
}
确实,我展示的样式是您应该编写代码的方式,其中JavaScript与HTML是分开的。当然,如果您坚持要求,则可以将函数包装在HTML的Anonymous函数中。错误的HTML做法:
onclick='function(){agentsBelow(\"you have String issues too\")}'
请注意,您不需要将双引号连接在一起。顺便一提:
onclick='agentsBelow(".$first_levelIdFunction.")'
使用不良做法,应为:
onclick='function(){agentsBelow(\"$first_levelIdFunction\")}'
您正在将变量而不是字符串传递给JavaScript。
onclick='agentsBelow(".$first_levelIdFunction.")'
可以评估为:
onclick='agentsBelow(youFirstLevelIdFuntionVarResultIsNotaString)'
但这仍然行不通,因为事件对象会传入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句