【问题标题】:How to change an svg image after onClick()?如何在 onClick() 之后更改 svg 图像?
【发布时间】:2021-06-11 18:56:51
【问题描述】:

我有一个 div 标签,其中包含一些文本和一个像 so 这样的 svg 图标

单击该选项卡后,它会像这样展开

一旦扩展,我希望 svg 图标更改为其他内容。到目前为止,我的代码没有给我任何错误,但也没有按预期工作。我目前有一个功能应该在单击元素后将图标更改为 icon-cancel.svg 没有任何变化。这是我所拥有的。

<!DOCTYPE html>
<html lang="en">
  <head>

    <style type="text/css">
      body {
        font-family: 'Roboto Condensed', sans-serif;
      }
      #side-chat {
        position: absolute;
        right: 100%;
        bottom:50%;
        z-index:9999999999999 !important;
        width: 150px;
        margin-right: -59px;
        transform: rotate(-90deg);
        display:flex;
        justify-content: center;
        align-items: center;
        color: #ffffff;
        border-radius: 10px;
        background: rgba(30, 175, 230, 0.5);
        text-decoration: none;
        padding: 15px;
        font-size: 25px;
        line-height: 20px;
        text-align: center;    
      }
      #olark-box-wrapper {
        position: absolute;
        z-index:99999999999999 !important;
        top: 400px;
        right: -300px;

        -webkit-transition-duration: 0.3s;
        -moz-transition-duration: 0.3s;
        -o-transition-duration: 0.3s;
        transition-duration: 0.3s;
      }
      #olark-box-wrapper.chatbox-open {
        right: 0
      }
      #olark-box-wrapper.chatbox-closed {
       right: -300px;
      }
      #habla_window_div {
        margin: 0 !important;
      }
      #side-chat img{
        margin-right: 10px;
        
      }
      #side-chat:hover,
      #side-chat:active {
       background: #22a7e5;
}
    </style>
  </head>
  <body>
<div id="olark-box-wrapper">

  <!-- Olark chat tab -->
    <a id="side-chat" href="javascript:void(0);" onclick="changeClass(); changeImage();">
      <img src="icon-chat.svg">
         Chat
    </a>

  <!-- Empty Olark chat box container -->
  <div id="olark-box-container"></div>

</div>

<!-- begin olark code -->
<script type="text/javascript" async> ;(function(o,l,a,r,k,y){if(o.olark)return; r="script";y=l.createElement(r);r=l.getElementsByTagName(r)[0]; y.async=1;y.src="//"+a;r.parentNode.insertBefore(y,r); y=o.olark=function(){k.s.push(arguments);k.t.push(+new Date)}; y.extend=function(i,j){y("extend",i,j)}; y.identify=function(i){y("identify",k.i=i)}; y.configure=function(i,j){y("configure",i,j);k.c[i]=j}; k=y._={s:[],t:[+new Date],c:{},l:a}; })(window,document,"static.olark.com/jsclient/loader.js");
  /* custom configuration goes here (www.olark.com/documentation) */
  //olark.configure('system.hb_detached', true);
  olark.configure('box.inline', true);
  olark.identify('xxxx-xxx-xx-xxxx');</script>
  <!-- end olark code -->
  <script type='text/javascript'>
    // Javacript function to toggle the class of the chat box wrapper
    function changeClass()
    {
      // Get the HTML object containing the Olark chat box
      var olark_wrapper = document.getElementById("olark-box-wrapper");
      // If the chat box is already open, close id
      if ( olark_wrapper.className.match(/(?:^|\s)chatbox-open(?!\S)/) ) {
        olark_wrapper.className = "chatbox-closed";
       
      }
      // Otherwise add open the Olark chat box
      else {        
        olark_wrapper.className = "chatbox-open";
        
      }
        
    }


  function changeImage(){
document.getElementById('side-chat').src = "icon-cancel.svg";
</script>
  </body>
</html>

【问题讨论】:

标签: javascript html css svg


【解决方案1】:

你的“side-chat”元素是&lt;a&gt; 标签,它没有src。 尝试将 getElementById 更改为 querySelector 以获取内部图像。

 function changeImage(){
     document.querySelector('#side-chat img').src = "icon-cancel.svg";
 }

【讨论】:

    【解决方案2】:

    打开 svg 文件中的代码后 小路 复制 svg 代码之间的所有内容。然后添加一个 onclick 事件。我认为如果您在单击时添加像 svg.innerHTML = what you copied 这样的单击事件,您的问题将得到解决。由于 svg 文件由矢量组成,因此其中的路径代码决定了图标。当它改变时,图标也会改变。希望能帮上忙。

    var svg = document.querySelector('.svg');
            svg.addEventListener('click',()=>{
                svg.innerHTML=`
               <path d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"></path>
                `
            })
      .svg
            {
                width: 30px;
                cursor: pointer;
            }
     <h2>Click svg</h2>
        <svg aria-hidden="true" class="svg" focusable="false" data-prefix="fas" data-icon="bars" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M16 132h416c8.837 0 16-7.163 16-16V76c0-8.837-7.163-16-16-16H16C7.163 60 0 67.163 0 76v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16z"></path></svg>

    【讨论】:

      【解决方案3】:

      在您的函数中,您正在更改 &lt;a&gt; 标记的 src。但是您想从&lt;img&gt; 标记更改它。所以你的选择器是错误的。

      document.getElementById('side-chat') - outputs the <a> tag
      

      只需在&lt;img&gt; 标签中添加一个 id 并选择这个 id。 那么你就可以 document.getElementById('imgId').src = "其他img路径" 在你的 changeImg() 函数中。

      【讨论】:

        猜你喜欢
        • 2023-03-03
        • 2011-09-18
        • 1970-01-01
        • 1970-01-01
        • 2023-01-26
        • 1970-01-01
        • 2021-09-15
        • 2020-09-20
        • 2020-12-16
        相关资源
        最近更新 更多