localStorage.setItem 不适用于 标签。

huangapple go评论68阅读模式
英文:

localStorage.setItem doesn't work with <img> tag

问题

我有一个JS函数,它从我的字典的特定字段中读取数据,并将读取的值设置到与它们相关联的标签上的新页面。

const good_render = id => {
  localStorage.setItem("name", goods[id].name);
  localStorage.setItem("price", goods[id].price);
  localStorage.setItem("img", goods[id].img);
};

我使用以下方式应用这些值:

<script>
  document.getElementById("name").textContent = localStorage.getItem("name");
  document.getElementById("price").textContent = localStorage.getItem("price");
  document.getElementById("img").src = localStorage.getItem("img");
</script>

它可以正常使用.textContent,但使用.src时出现问题。可能的问题是什么?

=更新=
简要来说,必须按下按钮才能跳转到产品页面。

产品页面本身:

<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" href="slider\itc-slider.css">
  <link rel="stylesheet" href="good_page.css">
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div>
    <h1 class="header1" id="name"></h1>
  </div>
  <div class="container">
    <div class="itc-slider" data-slider="itc-slider">
      <div class="itc-slider-wrapper">
        <div class="itc-slider-items">
          <div class="itc-slider-item">
            <img src="slider\examples\images\1.jpg">
          </div>
          <div class="itc-slider-item">
            <img src="slider\examples\images\2.jpg">
          </div>
          <div class="itc-slider-item">
            <img src="slider\examples\images\3.jpg">
          </div>
          <div class="itc-slider-item" id="img">
            <img src="slider\examples\images\4.jpg">
          </div>
        </div>
      </div>
      <button class="itc-slider-btn itc-slider-btn-prev"></button>
      <button class="itc-slider-btn itc-slider-btn-next"></button>
    </div>
  </div>
  <div>
    <div class="header1" id="price"></div>
    <div>
      <button class="button-3" role="button" id="cart">Add to cart</button>
    </div>
  </div>
  <script src="slider\itc-slider.js" defer></script>
  <script>
    document.getElementById("name").textContent = localStorage.getItem("name");
    document.getElementById("price").textContent = localStorage.getItem("price");
    document.getElementById("img").src = localStorage.getItem("img");
  </script>
</body>
</html>

执行的JS:

var goods = {
  "good1": {
    "name": "good1",
    "price": 100,
    "img": "https://static.winestreet.ru/off-line/goods_file/46001/file_S.jpg",
    "in_store": 1
  },
  "good2": {
    "name": "good2",
    "price": 50,
    "img": "https://pitersmoke.ru/wa-data/public/shop/products/90/15/11590/images/18550/18550.970.jpg",
    "in_store": 1
  }
};

document.onclick = event => {
  if (event.target.id == 'good1') {
    good_render(event.target.id);
  }

  if (event.target.id == 'good2') {
    good_render(event.target.id);
  }
};

const good_render = id => {
  localStorage.setItem("name", goods[id].name);
  localStorage.setItem("price", goods[id].price);
  localStorage.setItem("img", goods[id].img);
};
英文:

I got a JS function that reads from specific field of my dict and set the read values on new page to the tags they are related to.

const good_render = id =&gt; {
localStorage.setItem(&quot;name&quot;, (goods[id].name));
localStorage.setItem(&quot;price&quot;, (goods[id].price));
localStorage.setItem(&quot;img&quot;, (goods[id].img))};

I apply these values this way:

 &lt;script&gt;
document.getElementById(&quot;name&quot;).textContent = name = localStorage.getItem(&quot;name&quot;);
document.getElementById(&quot;price&quot;).textContent = price = localStorage.getItem(&quot;price&quot;);
document.getElementById(&quot;img&quot;).src = img = localStorage.getItem(&quot;img&quot;)
&lt;/script&gt;

It works perfectly fine with .textContend, however with .src it doesn't. What might be the problem?
=Update=
Briefly, a button must be pressed so to travel to a product page

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;main_2_buttons.css&quot;&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href=&quot;../good_page/good_page.html&quot; class=&quot;button-34&quot; id=&quot;good1&quot;&gt;good 1&lt;/a&gt;
&lt;a href=&quot;../good_page/good_page.html&quot; class=&quot;button-34&quot; id=&quot;good2&quot;&gt;good 2&lt;/a&gt;
&lt;script src=&quot;main_2_buttons.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

The product page itself

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;slider\itc-slider.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;good_page.css&quot;&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;h1 class=&quot;header1&quot; id = &quot;name&quot;&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;itc-slider&quot; data-slider=&quot;itc-slider&quot;&gt;
&lt;div class=&quot;itc-slider-wrapper&quot;&gt;
&lt;div class=&quot;itc-slider-items&quot;&gt;
&lt;div class=&quot;itc-slider-item&quot;&gt;
&lt;img src = &quot;slider\examples\images\1.jpg&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;itc-slider-item&quot;&gt;
&lt;img src = &quot;slider\examples\images\2.jpg&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;itc-slider-item&quot;&gt;
&lt;img src = &quot;slider\examples\images\3.jpg&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;itc-slider-item&quot; id =&quot;img&quot;&gt;
&lt;img src = &quot;slider\examples\images\4.jpg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;button class=&quot;itc-slider-btn itc-slider-btn-prev&quot;&lt;/button&gt;
&lt;button class=&quot;itc-slider-btn itc-slider-btn-next&quot;&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt; &lt;!--class=&quot;nl l1n&quot;--&gt;&gt;
&lt;div class=&quot;header1&quot; id = &quot;price&quot;&gt;&lt;/div&gt;
&lt;div&gt;
&lt;button class=&quot;button-3&quot; role=&quot;button&quot; id=&quot;cart&quot;&gt;Add to cart&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;slider\itc-slider.js&quot; defer&gt;&lt;/script&gt;
&lt;script&gt;
document.getElementById(&quot;name&quot;).textContent = name = localStorage.getItem(&quot;name&quot;);
document.getElementById(&quot;price&quot;).textContent = price = localStorage.getItem(&quot;price&quot;);
document.getElementById(&quot;img&quot;).src = img = localStorage.getItem(&quot;img&quot;)
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

And the executed JS

var goods = {                 
&quot;good1&quot; : {               
&quot;name&quot; : &quot;good1&quot;,
&quot;price&quot;: 100,
&quot;img&quot;  : &quot;https://static.winestreet.ru/off-line/goods_file/46001/file_S.jpg&quot;,
&quot;in_store&quot;: 1,
},
&quot;good2&quot; : {
&quot;name&quot; : &quot;good2&quot;,
&quot;price&quot;: 50,
&quot;img&quot;  : &quot;https://pitersmoke.ru/wa-data/public/shop/products/90/15/11590/images/18550/18550.970.jpg&quot;,
&quot;in_store&quot;: 1,
}
};
document.onclick = event =&gt; { 
if (event.target.id == &#39;good1&#39;) {
good_render(event.target.id);
}
if (event.target.id == &#39;good2&#39;) {
good_render(event.target.id);
}
};
const good_render = id =&gt; {
localStorage.setItem(&quot;name&quot;, (goods[id].name));
localStorage.setItem(&quot;price&quot;, (goods[id].price));
const imgURL = URL.createObjectURL(goods[id].img);
localStorage.setItem(&quot;img&quot;, imgURL);  
};

答案1

得分: 0

document.getElementById("img") 返回根据您的HTML问题而言的div元素,所以要么将id设置为您的img标签,而不是div,或者执行以下操作:

var myDivEle = document.getElementById("img"); // 这是一个<div>而不是<img />
myDivEle.getElementsByTagName('img')[0].src = localStorage.getItem("img");
英文:

document.getElementById(&quot;img&quot;) returns div element as per your HTML in question, so either set id to your img tag instead of div or do:

var myDivEle = document.getElementById(&quot;img&quot;); //its a &lt;div&gt; not &lt;img /&gt;
myDivEle.getElementsByTagName(&#39;img&#39;)[0].src = localStorage.getItem(&quot;img&quot;);

huangapple
  • 本文由 发表于 2023年6月15日 16:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定