我有一个链接,点击链接我想将一个标志变量的值设置为1.这可能吗?喜欢
<a id='ctr' href='#' onclick='flag=1'> Hello </a>
Run Code Online (Sandbox Code Playgroud)
以及如何在同一行上编写onclick事件的函数,而不是
<a id='ctr' href='#' onclick='call_this();'> Hello </a>
function call_this(){
flag=1;
}
Run Code Online (Sandbox Code Playgroud)
我可以在onclick行中使用call_this函数吗?
EDIT2
现在我有这样的代码.在onclick事件中,值正确地存储在$ selectedId中.但是当我尝试在url中传递该值时,我没有得到正确的值.传递for循环中的最后一个值.
<script type="text/javascript">
$(document).ready(function(){
<?php foreach ($Forms as $r): ?>
$("<a id='<?php echo $ctr['Form']['id'];?> href='#' onclick='flag=1'> Hello </a>").appendTo("#header").click(function(){
<?php $selectedId=$r['Form']['id'];?>
alert("selId: "+<?php echo $selectedId;?>);//here the selected value is echoed
});
</script>
<a href="http://localhost/Example/new/export?height=220&width=350&id=<?php echo $selectedId;?>" class="thickbox button" title= "Export" >Export</a>
Run Code Online (Sandbox Code Playgroud)
这里的id传递为4,这是php for循环中的最后一个值.
由于您的锚元素上有ID,因此您可以轻松找到它并绑定事件.
如果这是一个简单的操作,您可以使用匿名函数,而不必在全局范围内声明函数:
window.onload = function () {
// Bind events...
document.getElementById('ctr').onclick = function () {
flag1 = 1;
return false; // stop the browser from following the link
};
// ....
};
Run Code Online (Sandbox Code Playgroud)
编辑:看看你的编辑,jQuery document.ready事件在页面加载时触发,此时你的标志仍然已经null分配.
请注意,您只是将锚元素添加到DOM,然后创建一个if语句.
由于您使用jQuery,您可以使用它将click事件绑定到新创建的元素:
var flag=null;
$(document).ready(function(){
$('<a id="ctr" href="#"> Hello </a>').appendTo('#header').click(function () {
flag = 1;
e.preventDefault(); // stop the browser from following the link
});
});
Run Code Online (Sandbox Code Playgroud)
您可以这样做,因为appendTo函数返回新创建的DOM元素,您可以直接将事件绑定到它.
| 归档时间: |
|
| 查看次数: |
13653 次 |
| 最近记录: |