代码之家  ›  专栏  ›  技术社区  ›  seymar

使用javascript对:target执行一些操作

  •  1
  • seymar  · 技术社区  · 15 年前

    我在用CSS3 :target 伪选择器,用于在不重新加载页的情况下创建页内导航。这真的很管用!

    但是我有一个问题,我需要在页面目标化时重置页面中的表单,如何知道元素是否以javascript为目标?就像 element.ontarget = function();

    或者像这样 element.ondisplaychange -> element.oncsschange ?

    更好的更新:

    var hashcache = document.location.hash;
    window.onhashchange = function() {
        if(hashcache != document.location.hash) {
            $(hashcache + ' form input').each(function() {
                $(this).val('');
            });
            hashcache = document.location.hash;
        }
    }
    

    更新:

    $('a[href^="#"]').each(function() {
        this.onclick = function() {
            href = $(this).attr('href');
            if(href != document.location.hash) {
                $(href + ' form input').each(function() {
                    $(this).val('');
                });
            }
        }
    });
    3 回复  |  直到 15 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    如果您使用JavaScript进行导航,我建议您只添加检查。但我从你的问题中猜到你不是,你只是在使用简单的链接和锚(例如。, <a href='#target1'> , <a href='#target2'> , ...).

    有两种选择:

    使用计时器

    在这种情况下,基本上你想做的事情可以归结为当锚改变时接收一个事件。就我所知,就回答问题的人而言 this other question on StackOverflow 一月份就知道了,你只能用定时器。 (编辑:但是见下面ide的评论,有一个新的 hashchange 我们很快就可以使用的活动!) 例如。:

    (function() {
        var lastHash = window.location.hash;
        setTimeout(function() {
            var newHash = window.location.hash;
            if (newHash !== lastHash) {
                lastHash = newHash;
                // Trigger your target change stuff
            }
        }, 250);
    })();
    

    250 ,但要注意跑得太多,其他的都慢下来。

    但正如你下面所说,这是低效的。

    钩住链环 click 事件

    既然您已经在页面上使用了JavaScript,我建议您改为在链接上使用处理程序。如果您向它们添加类名或其他内容(我敢打赌它们已经有了一个类名;下面是“navlink”),则很容易设置:

    var links, index, link;
    links = document.getElementsByTagName('a');
    for (index = 0; index < links.length; ++index) {
        link = links.item(index);
        if ((" " + link.className + " ").indexOf(" navlink ") >= 0) {
            hookEvent(link, 'click', clickHandler);
        }
    }
    
    function clickHandler() {
        // `this` will reference the element that was clicked
    }
    
    // The 'hook' function:
    var hookEvent = (function() {
        var elm = document.createElement('a');
    
        function hookEventViaAttach(element, event, handler) {
            element.attachEvent("on" + event, handler);
        }
        function hookEventViaAddListener(element, event, handler) {
            element.addEventListener(event, handler, false);
        }
        function hookEventDOM0(element, event, handler) {
            element["on" + event.toLowerCase()] = handler;
        }
    
        if (elm.attachEvent) {
            return hookEventViaAttach;
        }
        if (elm.addEventListener) {
            return hookEventViaAddListener;
        }
        // I usually throw a failure here saying not supported, but if you want,
        // you can use the DOM0-style stuff.
        return hookEventDOM0;
    })();
    

    如果您使用类似于 jQuery , Prototype , YUI , Closure ,或 any of several others .

    例如,jQuery版本:

    $("a.navlink").click(clickHandler);
    function clickHandler() {
        // `this` will reference the element that was clicked
    }
    

    原型版本:

    $$("a.navlink").invoke('observe', 'click', clickHandler);
    function clickHandler() {
        // `this` will reference the element that was clicked
    }
    
        2
  •  0
  •   Greg    15 年前

    这个 onfocus 属性返回当前元素的onFocus事件处理程序代码。

    event handling code = element.onfocus
    

    这个 onblur 属性返回当前元素上存在的OnBurr事件处理程序代码(如果有的话)。

    element.onblur = function;
    

    例子: http://jsfiddle.net/g105b/cGHF7/

    <html>
    
    <head>
        <title>onblur event example</title>
    
        <script type="text/javascript">
    
        var elem = null;
    
        function initElement()
         {
         elem = document.getElementById("foo");
         // NOTE: doEvent(); or doEvent(param); will NOT work here.
         // Must be a reference to a function name, not a function call.
         elem.onblur = doEvent;
         };
    
        function doEvent()
         {
         elem.value = 'Bye-Bye';
         alert("onblur Event detected!")
         }
        </script>
    
        <style type="text/css">
        <!--
        #foo {
        border: solid blue 2px;
        }
        -->
        </style>
    </head>
    
    <body onload="initElement()";>
        <form>
        <input type="text" id="foo" value="Hello!" />
        </form>
    
        <p>Click on the above element to give it focus, then click outside the
        element.<br /> Reload the page from the NavBar.</p>
    
    </body>
    </html>
    
        3
  •  0
  •   Defims    15 年前

    也许你可以这样编码

    function hashChangeEvent(){
        $(window.location.hash)//do something
    }
    window.onhashchange = hashChangeEvent;//when hash change
    hashChangeEvent();//first load
    
    推荐文章