使用点击处理程序修改段落的 JavaScript 类

标签 javascript html css

创建一个 JavaScript 类“ModifyParagraph”,这里的构造函数接受段落元素。在构造函数中,使用 document.createElement 创建 4 个按钮(更改段落、粗体、颜色、大小、位置),并在每个按钮上添加点击事件处理程序。单击粗体按钮上的处理程序将打开/关闭粗体。

我的代码:

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Question-2</title>
        <meta charset="UTF-8">
        <script src="q2.js"></script>
        <link rel="stylesheet" href="q2.css" />
    </head>
    <body>
            <div id="myDIV" style="width:500px; ">Lorem ipsum dolor sit amet, porttitor ut donec ut egestas, purus urna consectetuer, arcu et vitae elementum nulla etiam in, sit leo lacus. Ligula cras tortor dignissim laoreet ut, nonummy hendrerit mauris, vitae augue congue congue, vel et augue, eros at. Fringilla id donec libero mauris eleifend. Nulla nunc sit. Consequat sodales placerat faucibus mauris, urna lectus vitae, sit nisl laoreet libero at suscipit ut, neque laoreet dapibus sapien. Sodales sit ut metus commodo tellus, ultricies cursus, et faucibus vitae quam, sit ultrices rhoncus. Massa duis mauris arcu vulputate, iste orci porttitor a ac, quisque venenatis pellentesque in in velit sed, repellat lorem consectetur vero, urna tellus donec. Suscipit in, donec beatae, lectus bibendum morbi justo consectetuer ac, facilisis metus vel non sapien vel. Magna congue vitae quia nulla nulla, lorem enim urna augue et et, sem morbi lorem ornare velit neque morbi, erat id et blandit iaculis.</div>

    </body>

    </ul>
</html>

JavaScript

class ModifyParagraph{
    constructor(myDIV){
        this.myDIV = myDIV;
    function myFunction(){
        var btn = document.createElement("BUTTON");
        var bold = document.getElementById(myDIV);
        btn.addEventListener('click', function(){
            if (bold.style['font-weight']) {
                bold.style.removeProperty('font-weight');
              } else {
                bold.style['font-weight'] = "800";
              }
          }
        }
    }
}

在我的代码中,我在构造函数中创建按钮并在每个按钮上添加点击处理程序时遇到问题。请让我知道如何进行

最佳答案

除了您的语法错误(我将在稍后的回答中介绍)之外,您的主要问题是您没有将创建的 button 附加到 DOM

这是一个工作演示:

class ModifyParagraph {
  constructor(div) {
    this.myDIV = div;
  }
  toggleBoldStyle() {
    // create the button.
    var btn = document.createElement('button');
    // give it some text/value.
    btn.textContent = 'toggle Bold styling';
    // add click event listener to the button.
    btn.addEventListener('click', function() {
      // if the font-weight is 800 make it 'normal' else if it's other than 800 make it 800'
      // add the 'this' keyword in the addEventListener method references the button, to get the member myDIV we'll just call it without preceding it by 'this'(myDIV in place of this.myDIV).
      myDIV.style.fontWeight = (myDIV.style.fontWeight === '800') ? 'normal':'800';
    });
    // append the button to the DOM
    this.myDIV.appendChild(btn);
  }
}
// testing...
var p = new ModifyParagraph(document.getElementById('myDIV'));
p.toggleBoldStyle();
<div id="myDIV" style="width:500px; ">Lorem ipsum dolor sit amet, porttitor ut donec ut egestas, purus urna consectetuer, arcu et vitae elementum nulla etiam in, sit leo lacus. Ligula cras tortor dignissim laoreet ut, nonummy hendrerit mauris, vitae augue congue congue, vel et augue, eros at. Fringilla id donec libero mauris eleifend. Nulla nunc sit. Consequat sodales placerat faucibus mauris, urna lectus vitae, sit nisl laoreet libero at suscipit ut, neque laoreet dapibus sapien. Sodales sit ut metus commodo tellus, ultricies cursus, et faucibus vitae quam, sit ultrices rhoncus. Massa duis mauris arcu vulputate, iste orci porttitor a ac, quisque venenatis pellentesque in in velit sed, repellat lorem consectetur vero, urna tellus donec. Suscipit in, donec beatae, lectus bibendum morbi justo consectetuer ac, facilisis metus vel non sapien vel. Magna congue vitae quia nulla nulla, lorem enim urna augue et et, sem morbi lorem ornare velit neque morbi, erat id et blandit iaculis.</div>

除了没有将 button 附加到 DOM 之外,您还有一些错误:

  • 类方法不需要'function'关键字,你只需要写方法名。
  • 您错过了 addEventListener 方法的右括号。
  • 为什么要尝试获取刚刚保存在 muyDIV 成员中的元素。更好的是,您有两种选择:将元素本身发送给构造函数或发送它的 ID 并在分配给 myDIV 成员时通过 getElementById 获取元素 方法。在我的演示中,我将元素本身发送给构造函数。但是,您可以使事情变得更复杂以允许发送 ID 或元素本身成为可能,在构造函数中您必须检查传入参数的类型。

最后,这是 link ECMAScript 5 类的 MDN 文档。

希望我能进一步插入你。

关于使用点击处理程序修改段落的 JavaScript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580613/

相关文章:

html css Gif 动画

html - 如何使用正则表达式获取选定的选项文本?

html - 我的 Django 模板中的问题居中页脚

javascript - ng-model 具有字符串值和 ng-option 整数

javascript - API 是否应该包含用户信息?

javascript - 如何以功能样式仅更改对象数组中的一个特定对象?

javascript - 显示当前图像时,从阵列内的剩余图像中随机选择

javascript - 一旦点击链接或刷新页面,Jquery的toggleClass就会丢失类

css - 如何在不滚动后台页面内容的情况下滚动菜单?

jquery - 禁用特定字段的切换功能