【问题标题】:Direct link to the content直接链接到内容
【发布时间】:2011-09-19 02:49:58
【问题描述】:

我有一个页面http://bartle96.narod2.ru/demo.html有4个隐藏内容的链接

是否可以直接链接到“海豚”等内容。

这样当你点击http://bartle96.narod2.ru/demo.html#delfin的链接时会立即打开一张与海豚的照片

【问题讨论】:

标签: javascript


【解决方案1】:

是的,您可以在onload 事件中定义和打开所需的内容。

查看这篇文章,您可以在其中找到相同的行为:

jQuery when pointed to a link should show a div that's hidden by default

【讨论】:

    【解决方案2】:

    您可以使用window.location.hash 获取哈希值。然后根据值设置你想要的图片。

    window.onload = function() {
      var pic = window.location.hash;
      if ( pic === "#delfin" ) {
        // Show picture of dolphin
      }
    }
    

    【讨论】:

    • 这只是对 OP 技术的评论,但最好隐藏显示的 DIV,而不是通过 STYLE 属性。这样,如果 JS 不起作用,内容仍然是可见的。
    • 另外,我怀疑是否需要 if 声明,我认为 OP 只是作为一个例子。 (当然,这实际上不是那么清楚,但是当它可以调用article(hashbang) 时,这在实践中将是一种糟糕的技术)
    猜你喜欢
    • 2017-06-16
    • 2018-07-13
    • 2011-01-11
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2018-02-08
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多