当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


HTML DOM blockquote用法及代码示例

HTML DOM blockquote 本质上代表 HTML 元素 <blockquote>。与 <q> 标记不同,<blockquote> 元素不添加任何引号。我们可以在 blockquote 对象的帮助下创建和访问 <blockquote> 属性。

用法

以下是语法 -

创建块引用对象 -

var x = document.createElement("BLOCKQUOTE");

示例

让我们看一个 blockquote 对象的例子 -

<!DOCTYPE html>
<html>
<body>
<p>Click on the below button to create a blockquote object</p>
<button onclick="createBloc()">CREATE</button>
<script>
   function createBloc() {
      var x = document.createElement("BLOCKQUOTE");
      var t = document.createTextNode("This is a random block quote.This is some sample text.");
      x.setAttribute("cite", "http://www.examplesite.com");
      x.setAttribute("id", "myQuote");
      x.appendChild(t);
      document.body.appendChild(x);
   }
</script>
</body>
</html>

输出

这将产生以下输出 -

单击创建 -

在上面的例子中 -

首先,我们创建了一个按钮 CREATE 来调用 createBloc() 函数。

<button onclick="createBloc()">CREATE</button>

函数 createBloc() 使用文档对象的 createElement() 方法来创建 <blockquote> 元素。然后使用文档对象的 createTextNode() 方法,我们创建了一个文本节点,其中包含一些文本。使用 setAttribute() 方法,我们向上面创建的 <blockquote> 元素添加一些属性,如 cite 和 id。然后使用 appendChild() 方法最终将该元素作为子元素附加到文档正文中。

function createBloc() {
   var x = document.createElement("BLOCKQUOTE");
   var t = document.createTextNode("This is a random block quote.This is some sample text.");
   x.setAttribute("cite", "http://www.examplesite.com");
   x.setAttribute("id", "myQuote");
   x.appendChild(t);
   document.body.appendChild(x);
}

相关用法


注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 HTML DOM blockquote object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。