【问题标题】:How can I retrieve an image path from another page using JS?如何使用 JS 从另一个页面检索图像路径?
【发布时间】:2012-03-26 00:55:43
【问题描述】:

这是我的情况:我有一个带有会经常更新的横幅的页面,这意味着每次更新页面时横幅的图像路径都会不同(不能被覆盖)。无论如何,它将位于具有常量名称的 div 或容器中。

我需要做的是检索该图像路径并将其打印在不同的页面中,因此如果横幅在第一页中发生变化,它将在第二页中自动更改。

我认为也许一些 javascript 可以完成这项工作,但我不确定如何从 div 中获取图像路径。

任何帮助将不胜感激,来自阿根廷的问候

【问题讨论】:

  • 你会用jQuery吗?如果是,您可能会在 div 框架内获得第一个 标记(您知道它的 ID)并读取 src 属性值。
  • 您能否发布一些示例 HTML 以帮助人们提供相关答案?
  • 是的,其实很简单,我只需要从这样的 div 中检索图像:(我不知道如何添加代码格式)
  • 是的,我也可以使用 jQuery :)

标签: javascript html image path


【解决方案1】:

使用html5和javascript的解决方案是这样的

  you can get the image tag through javascript(as u say it is in div
  and whose id you know)  
  something like
  src = document.getElementById("id").childnodes[0].src
  should work for u

  then you can store this src in the localStorage

  localStorage["src"] = src;

  as soon as you store something in localstorage it will fire a 
  "storage" event in all the other pages except the page in which 
  you have actually stored the src


 so handle "storage" event in the other pages like this

     window.addEventListener("storage",func,false);

     function func(event)
     {

         src  = localStorage[event.key];
        //now src has the updated src :)
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2017-09-13
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多