简体   繁体   English

我可以使用document.getElementById(someid).onclick标记a

[英]can I use document.getElementById(someid).onclick for tag a

I am trying to call a javascript function onclick. 我试图调用一个javascript函数onclick。 I have written something like this 我写过这样的东西

<script type="text/javascript">
    function readPage(){
        alert("Hello");
    }

    document.getElementById('read').onclick=readPage;
</script>

<a id="read" href="">read</a> 

I am trying to call readPage function but its not working?if I write onclick inside tag it works but the way I have written above is not working. 我试图调用readPage函数,但它不起作用?如果我写onclick里面标签它的工作原理,但我上面写的方式不起作用。 why? 为什么?

There is nothing wrong about how you do it, but when. 你是怎么做的,但是什么时候没有错。 You can not access the DOM (like running getElementById()) before it has loaded. 在加载之前,您无法访问DOM (like running getElementById()) The easiest thing to do is to run you code inside window.onload like this: 最简单的方法是在window.onload运行代码,如下所示:

window.onload = function () {
   document.getElementById("read").onclick=readPage;
};

It will work with an empty href attribute (then the current URL of the page will be used), but you have to use, as already mentioned, window.onload to attach the click handler, or you have to move the script block after the a element. 它将使用一个空的href属性(然后将使用页面的当前URL),但是你必须使用window.onload来附加点击处理程序,或者你必须在之后移动script a元素。

Otherwise getElementById cannot find the element because it does not yet exist in the DOM tree. 否则getElementById找不到该元素,因为它在DOM树中尚不存在。

<a id="read" href="">read</a> 

<script type="text/javascript">
    function readPage(){
        alert("Hello");
        return false;
    }

    document.getElementById('read').onclick=readPage;
</script>

​ As already mentioned, you use eg return false; 如前所述,您使用例如return false; to make the browser not follow the URL. 使浏览器遵循URL。 You even need this if you change the URL to href="#" because otherwise the browser will scroll the page to the top. 如果您将URL更改为href="#"您甚至需要这个,否则浏览器会将页面滚动到顶部。

Test it yourself: http://jsfiddle.net/Nj4Dh/1/ 自己测试一下: http//jsfiddle.net/Nj4Dh/1/


Read more about the traditional event registration model . 阅读有关传统事件注册模型的更多信息。

You would first have to disable anchor's default direction protocol (ie, not let a go to href) 你首先必须禁用锚的默认方向协议(即,不要去一个href)

  1. Make href="#" on anchor. 在锚点上创建href="#"
  2. Check the event.preventDefault() function example here . 这里检查event.preventDefault()函数示例。

You should have: 你应该有:

<script type="text/javascript">
    function readPage(){
        alert("Hello");
    }

    document.getElementById('read').onclick=readPage;
</script>

<a id="read" href="#">read</a> 

There are two ways to do it really (Without any browser specific functions). 有两种方法可以做到(没有任何浏览器特定的功能)。 One is 一个是

<script type="text/javascript">

    function stopDefault(e) //cross browser function for stopping default actionw
    {
        if (e && e.preventDefault)
        {
            e.preventDefault();
        }
        else
        {
            window.event.returnValue = true;
        }
        return false;
    }

    function readPage(event){
        alert("Hello");

         return stopDefault(event);//prevent default action
    }



    window.onload = function(event){

         document.getElementById('read').onclick=readPage;


    }
</script>

<a id="read" href="#">read</a> 

Or: 要么:

<script type="text/javascript">
    function stopDefault(e)
    {
        if (e && e.preventDefault)
        {
            e.preventDefault();
        }
        else
        {
            window.event.returnValue = true;
        }
        return false;
    }

    function readPage(event){
        alert("Hello");

         return stopDefault(event);//prevent default action
    }

</script>
<a id="read" href="#" onclick="readPage(event)">read</a> 

I've used the stopDefault function sd You need to stop the default action of a link, otherwise it will try to go to the url thats in the href. 我已经使用了stopDefault函数sd你需要停止链接的默认动作,否则它会尝试转到href中的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM