Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@wingmeng

Description

@wingmeng
Issue body actions

题目:

已知有如下 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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.