【问题标题】:How to toggle src attribute of script when user click element?用户单击元素时如何切换脚本的 src 属性?
【发布时间】:2018-03-17 03:36:11
【问题描述】:

我在我的 Django 项目中使用 bootstrap 3.3.7。我有<textarea> 的模态。在角落的那个文本区域内,我放了一个图标。下面是我的文本区域的图片。 当用户单击图标时,我将 textarea 大小更改为浏览器的全屏。另外,我在我的 textarea 上使用了autosize 插件,它可以调整 textarea 的高度以适应文本。

在正常情况下(当 textarea 很小时)我需要使用 autosize 插件。当我单击图标并将文本区域更改为全屏时,我不需要使用此插件。

问题:点击图标时如何切换脚本的src属性?

P.S. 当 textarea 处于全尺寸模式时,我与 autosize 插件几乎没有冲突。因为当我开始以全尺寸模式在 textarea 中写入时,autosize 插件会改变它的高度。这就是为什么我想在 textarea 处于全屏模式时取消这个插件。

autosize(document.querySelectorAll('#body')); // Autosize for textarea with "body" id

$('.textarea-icon').click(function() {
  $(".textarea-icon").toggleClass("fa-expand fa-compress")
  $(".textarea-wrapper").toggleClass('textarea-wrapper-fullscreen')
  $("#body").toggleClass('textarea-fullscreen')
  $(".modal-dialog").toggleClass('modal-dialog-fullscreen')
  $(".textarea-icon").toggleClass('textarea-icon-fullscreen')
});
.dashboard-wrapper {
  position: relative;
}

#dashboard-content {
  float: right;
  width: 100%;
  margin-top: 51px;
}

.textarea-wrap {
  position: relative;
  display: inline-block;
  width: 100%;
}

.textarea-wrap .textarea-icon {
  position: absolute;
  top: 3px;
  right: 3px;
  color: #BDBDBD;
  text-decoration: none;
  font-size: 17px;
  outline: none;
  cursor: pointer
}

.textarea-wrap .textarea-icon:hover {
  color: #757575;
  outline: none;
}

.textarea-wrap.textarea-wrap-fullscreen {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.textarea-wrap textarea.textarea-fullscreen {
  margin-bottom: 30px;
  overflow-x: hidden;
  overflow-wrap: break-word;
  height: 100vh;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/autosize@4.0.0/dist/autosize.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="dashboard-wrapper">
  <div id="dashboard-content">
    <div class="modal fade">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <form method="post" action="">
            <div class="form-group">
              <label for="body">Discription</label>
              <div class="textarea-wrapper">
                <textarea name="body" class="form-control" id="body"></textarea>
                <i class="fa fa-arrows textarea-icon" aria-hidden="true"></i>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script id="" src="{% static 'js/autosize.js'%}"></script>

编辑

autosize(document.querySelectorAll('#body')); // Autosize to textarea

$('.textarea-wrapper .fa-expand').click(function() {
    autosize.destroy(document.querySelectorAll('#body')); // Destroy Autosize to textarea when user click icon
});

$('.textarea-icon').click(function() {
   $(".textarea-icon").toggleClass("fa-expand fa-compress") // Change Icon Picture
   $(this).closest('.textarea-wrapper').toggleClass('fullscreen') // Change textarea size
});

$('.textarea-wrapper .fa-compress').click(function() { // DONT WORK
   autosize(document.querySelectorAll('#body'));
})

【问题讨论】:

  • 在脚本运行后删除它不会在全屏时禁用插件。
  • 这与 bootstrap 或 django 没有任何关系

标签: javascript jquery html css django


【解决方案1】:

如果我理解你的话,你只想在放大它时在适当的元素上使用 autosize.destroy() (即在“点击”事件中,当你改变你的 textarea 行为时)。基本上它应该禁用在你的 textarea 上工作的插件。

(次要的旁注) 除此之外,我会投票赞成在 textarea 周围的包装器中添加一些特定的类,然后根据该类调整 css,这样您就可以避免在“点击”功能中对 DOM 进行一些查询。

【讨论】:

  • RE: 旁注 .... 肯定要走的路... jsfiddle.net/sheriffderek/e2hnhtu3 - 另外,如果你打算使用 jQuery,你也可以将它用于你所有的“查询” ' vs document.querySelectorAll('#body') --- 还...查看除 'div' 之外的其他一些元素,以便您的标记更具表现力。 :)
  • 我对你的回答有点困惑。你能更详细地谈谈你的想法吗?你对此有什么想法吗?在正常情况下(当 textarea 很小时)我需要使用 autosize 插件。当我单击图标并将 textarea 更改为全屏时,我不需要使用此插件。
  • 从文档中我发现了这个命令:autosize.destroy(document.querySelectorAll('#body')); 如何通过单击图标来切换自动调整大小?有什么想法吗?
  • 你能再看看我的帖子吗? EDIT 部分。我的代码的最后一部分不起作用。我认为导致 DOM 在图标类更改后看不到fa-compress。你有什么建议?
猜你喜欢
  • 1970-01-01
  • 2019-12-14
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
相关资源
最近更新 更多