Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript如何在DIV中打印标签?_Javascript_Html - Fatal编程技术网

JavaScript如何在DIV中打印标签?

JavaScript如何在DIV中打印标签?,javascript,html,Javascript,Html,例如,我有4个标有a1、a2、a3、a4的盒子。当有人点击两个框时,我希望标签(以a1、a2为例)打印在html输出上。我只花了一个多小时,我能想出的最好办法就是打印未定义和空 对不起,这是我的密码 HTML <div class="container"> <div class="row"> <div class="col-md-3" label="a1">

例如,我有4个标有a1、a2、a3、a4的盒子。当有人点击两个框时,我希望标签(以a1、a2为例)打印在html输出上。我只花了一个多小时,我能想出的最好办法就是打印未定义和空

对不起,这是我的密码

HTML
<div class="container">
  <div class="row">
    <div class="col-md-3" label="a1">
      <a class="btn btn-primary" href="#" role="button">Button 01</a>
      <div class="output"></div>
    </div>
    <div class="col-md-3" label="a2">
      <a class="btn btn-primary" href="#" role="button">Button 02</a>
      <div class="output"></div>
    </div>
    <div class="col-md-3" label="a3">
      <a class="btn btn-primary" href="#" role="button">Button 03</a>
      <div class="output"></div>
    </div>
    <div class="col-md-3" label="a4">
      <a class="btn btn-primary" href="#" role="button">Button 04</a>
      <div class="output"></div>
    </div>
  </div>
</div>

标签
实际上不是
标签的标准属性。如果您只是在寻找快速解决方案,可以尝试
id
。而且,你正在以一种非常奇怪的方式访问一切

  • 您应该将
    标签
    更改为
    id
    id
    属性对于所有HTML元素(据我所知)都是通用的,它允许您唯一地标识该元素
  • 
    
  • 向所有要作为“输出”的
    div
    元素添加唯一的
    id
    。这将允许您的代码将“输出”指向正确的元素
  • 
    
  • 最后,对JavaScript进行一些更改。您将看到的第一个更改是我将
    document.querySelector('.btn')
    更改为
    document.querySelector('.btn')
    。这些方法之间的区别在于,第一个方法只选择它找到的与选择器匹配的第一个元素,而第二个方法选择与选择器匹配的所有元素并创建一个数组
  • 接下来,我们循环该数组,为每个元素添加一个事件侦听器

    之后,我们将参数
    e
    (对于事件)添加到
    printLabel()
    函数中,因为
    addEventListener()
    在回调函数中传递事件对象(
    printLabel
    )。此对象提供与事件相关的目标元素的信息

    接下来,我们获取事件的目标元素,这就是您的
    按钮。然后我们得到
    按钮的
    parentElement
    ,因为您的
    id
    或“label”位于父元素上。然后,您可以从父元素的
    id
    获取名称

    请注意,
    id
    属性不能有空格或
    或除
    之外的大多数特殊字符

    最后,我们需要选择“output”元素,我们将使用
    id
    来实现这一点

    document.querySelector('#'+name+'-output')
    将获取具有给定
    名称的
    id
    元素
    +
    -output
    。例如,如果单击按钮
    a1
    ,这将获得
    id
    a1输出的元素。
    #
    表示您正在搜索
    id

    现在我们将该元素存储在变量
    print
    中,我们可以使用
    innerHTML
    属性将文本放置在其中

    const-button=document.querySelectorAll('.btn');
    对于(变量i=0;i
    我创建了一个帮助你的


    如果您有任何问题,请告诉我。

    标签
    实际上不是
    标签的标准属性。如果您只是在寻找快速解决方案,可以尝试
    id
    。而且,你正在以一种非常奇怪的方式访问一切

        <script>
            function printDiv(divName){
                var printContents = document.getElementById(divName).innerHTML;
                var originalContents = document.body.innerHTML;
    
                document.body.innerHTML = printContents;
    
                window.print();
    
                document.body.innerHTML = originalContents;
    
            }
        </script>
    <h1> do not print this </h1>
    
    <div id='printMe'>
      Print this only 
    </div>
    
    <button onclick="printDiv('printMe')">Print only the above div</button>
    
  • 您应该将
    标签
    更改为
    id
    id
    属性对于所有HTML元素(据我所知)都是通用的,它允许您唯一地标识该元素
  • 
    
  • 向所有要作为“输出”的
    div
    元素添加唯一的
    id
    。这将允许您的代码将“输出”指向正确的元素
  • 
    
  • 最后,对JavaScript进行一些更改。您将看到的第一个更改是我将
    document.querySelector('.btn')
    更改为
    document.querySelector('.btn')
    。这些方法之间的区别在于,第一个方法只选择它找到的与选择器匹配的第一个元素,而第二个方法选择与选择器匹配的所有元素并创建一个数组
  • 接下来,我们循环该数组,为每个元素添加一个事件侦听器

    之后,我们将参数
    e
    (对于事件)添加到
    printLabel()
    函数中,因为
    addEventListener()
    在回调函数中传递事件对象(
    printLabel
    )。此对象提供与事件相关的目标元素的信息

    接下来,我们获取事件的目标元素,这就是您的
    按钮。然后我们得到
    按钮的
    parentElement
    ,因为您的
    id
    或“label”位于父元素上。然后,您可以从父元素的
    id
    获取名称

    请注意,
    id
    属性不能有空格或
    或除
    之外的大多数特殊字符

    最后,我们需要选择“output”元素,我们将使用
    id
    来实现这一点

    document.querySelector('#'+name+'-output')
    将获取具有给定
    名称的
    id
    元素
    +
    -output
    。例如,如果单击按钮
    a1
    ,将获得id为
    id的元素
    
        <script>
            function printDiv(divName){
                var printContents = document.getElementById(divName).innerHTML;
                var originalContents = document.body.innerHTML;
    
                document.body.innerHTML = printContents;
    
                window.print();
    
                document.body.innerHTML = originalContents;
    
            }
        </script>
    <h1> do not print this </h1>
    
    <div id='printMe'>
      Print this only 
    </div>
    
    <button onclick="printDiv('printMe')">Print only the above div</button>
    
    <script>
      //getting all buttons
      var buttons = document.getElementsByClassName("btn");
      //adding event listner to all buttons
      for (var i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener("click", printLabel, false);
      }
      function printLabel() {
        const outputDiv = this.parentElement.querySelector(".output"); // this will select only closet div with given class
        outputDiv.innerText = this.parentElement.getAttribute("label");
      }
    </script>