javascript - 如何有选择地将脚本应用于元素?

标签 javascript html css

我有一个脚本可以使文本区域扩展以适应其中的文本。我希望这适用于我网站上的所有文本区域,但一个除外。无论如何要停止一个元素的脚本。

正在使用的脚本:

var autoExpand = function (field) {

// Reset field height
field.style.height = 'inherit';

// Get the computed styles for the element
var computed = window.getComputedStyle(field);

  // Calculate the height
  var height = parseInt(computed.getPropertyValue('border-top-width'), 10)
             + parseInt(computed.getPropertyValue('padding-top'), 10)
             + field.scrollHeight
             + parseInt(computed.getPropertyValue('padding-bottom'), 10)
             + parseInt(computed.getPropertyValue('border-bottom-width'), 
  10);

  field.style.height = height + 'px';

  };

  document.addEventListener('input', function (event) {
  if (event.target.tagName.toLowerCase() !== 'textarea') return;
  autoExpand(event.target);
  }, false);

文本区域:

<textarea id="sendie" maxlength = '100' ></textarea>

最佳答案

CSS

textarea {
  resize: none;
}

按编号:

#sendie {
  resize: none;
}

那行不通,把它应用到你的函数中

  document.addEventListener('input', function (event) {
      if (event.target.tagName.toLowerCase() !== 'textarea') return;
      autoExpand(event.target);
      document.getElementById("sendie").style.resize = "none"
  }, false);

假设“sendie”是您不想招惹的那个。

关于javascript - 如何有选择地将脚本应用于元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55525946/

相关文章:

html - 为什么我的 FontAwesome 内容作为背景出现在中心之外?

html - 提供 XHTML 而不是 HTML 不好吗?

php - 无法使用 PHP 上传文件

javascript - 带 href 的 SVG animateTransform

javascript - 异步函数上的setTimeout

javascript - 在 jQuery 工具提示中使用动态 ID 渲染 Highchart

css - 内部 div 被推出父 div

javascript - 如何使用 setTimeout 添加和删除 CSS 类 n 次?

html - 更改背景图像的参数 : url at runtime css

javascript - 它没有记录我悬停在一个元素上