Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 23 期(ECMAScript-语法):字符串对象HTML格式替代方法 #26

Open
wingmeng opened this issue Jun 1, 2019 · 0 comments
Open

Comments

@wingmeng
Copy link
Collaborator

wingmeng commented Jun 1, 2019

题目:

已知有如下 HTML 结构,请编写函数 format,实现点击对应按钮后对 #str 文本进行加粗、斜体、上标或下标格式化。

提示:请使用 HTML 格式替代方法

<span id="str">儿童节快乐!</span>
<p>
  <button onclick="format(1)">加粗</button>
  <button onclick="format(2)">斜体</button>
  <button onclick="format(3)">上标</button>
  <button onclick="format(4)">下标</button>
</p>

参考答案:

function format(type) {
  let text = str.innerText || str.textContent;
  let result;

  switch(type) {
    case 1:
      result = text.bold();
      break;
    case 2:
      result = text.italics();
      break;
    case 3:
      result = text.sup();
      break;
    case 4:
      result = text.sub();
      break;      
    default:
      result = text;  
  }

  str.innerHTML = result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant