RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 764403
Accepted
Randall
Randall
Asked:2020-12-28 18:56:44 +0000 UTC2020-12-28 18:56:44 +0000 UTC 2020-12-28 18:56:44 +0000 UTC

如何在点击时复制代码块

  • 772

$(".room-guests-count").click(function(e){
	$(".form-open").toggle();
	e.stopPropagation();
});
$('.form-open').click(function(e) {
  e.stopPropagation();
});
$(document).click(function() {
  $('.form-open').hide();
});
//код которое должен добавить дубликат блока 
$("#add-room").on("click", function(){
//Получаем имя и комментарий из инпутов
 var adultsChildren = $(".adults-children-wrapper");
 //Добавляем результат к нужному блоку
 $('.adults-children').add("<div class='adults-children-wrapper'></div>");
});
body {
  background:lightblue;
}
/*ROOM GUESTS COUNT*/
.room-guests-count {
	width: 34%;
	cursor: pointer;
	margin-right: 4%;
	background: #fff;
	height: 66px;
	margin-right: 152px;
}
.form-open {
	display: none;
	position: absolute;
	z-index: -1;
	top: 83px;
	background-color: #fff;
	height: auto;
	left: 9px;
	width: 348px;
}

.form-open:after {
	content: '';
	position: absolute;
	width: 0;
	height: 0;
	border-bottom: 10px solid #fff;
	border-left: 10px solid transparent;
	border-right: 10px solid transparent;
	right: 300px;
	bottom: 93px;
}
.room-guests-content {
	padding-top: 23px;
	padding-left: 15px;
}
.click_hidden_text {
	position: absolute;
	z-index: 1;
	left: 163px;
	top: 73px;
	opacity: 0.8;
}
.form-search {
	padding: 0;
	margin: 10px 10px 5px 10px;
	
}
.form-item {
	padding: 10px 5px;
	clear: both;
	height: 14px;
}
.edit-rooms {
	font-weight: normal;
	font-size: 13px;
	float: left;
	color: #000;
	margin-right: 5px;
}
.rooms-guests-plus {
	float: right;
	color: #fff;
	background:#ccc;
	width: 24px;
	height: 24px;
	text-align: center;
	line-height: 24px;
	top: -6px;
	position: relative;
	font-size: 18px;
	cursor: pointer;
}
.form-text {
	text-align: center;
	width: 6%;
	width: 20px !important;
	text-decoration: center;
	float: right;
	border:0;
	padding: 0;
	height: auto;
	outline: 0;
	vertical-align: middle;
}
.textfield label {
	margin-right: 2.5px;
	display: inline;
	font-size: 13px;
}
.children-1 label {
	display: inline;
	font-size: 13px;
}
.rooms-guests-minus {
	float: right;
	color: #fff;
	background:#e7e7e7;
	width: 24px;
	height: 24px;
	text-align: center;
	line-height: 24px;
	top: -6px;
	position: relative;
	font-size: 18px;
	cursor:auto;
}
.adults-children {
	clear: both;
}
.rooms-id {
	line-height: 40px;
	float: left;
	padding:5px 0 0 15px;
	color: #000;
}
.already-min {
	float: right;
	margin: -16px 0px 2px 6px;
	padding: 0;
	background: #fff;
}
.already-min-search {
	float: right;
	margin: 5px 10px 5px 2px;
}
.adults-children-wrapper {
	clear: both;
}
.children-1 {
	padding: 10px 5px;
	clear: both;
	height: 14px;
}
.edit-children {
	font-weight: normal;
	font-size: 13px;
	float: left;
	color: #000;
	margin-right: 5px;
}
.textfield {
	clear: both;
	height: 14px;
	margin: 0;
	position: relative;
	margin-top: 32px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="room-guests-count">
  <div class="room-guests-content">
    Взрослых
    <span class="ga-count">2</span>
    , Детей 
    <span class="children-count">0</span>
  </div>
</div>
<div class="form-open">
  <div class="form-search">
    <div class="form-item">
      <label class="edit-rooms">Количество номеров</label>
      <div class="rooms-guests-plus" id="add-room">+</div>
      <input type="text" name="rooms" max="9" min="1" maxlength="128" class="form-text" value="1" size="60" readonly="readonly">
      <div class="rooms-guests-minus">-</div>
    </div>
  </div>
  <div class="adults-children">
  <div class="adults-children-wrapper">
    <div class="rooms-id">Номер 1:</div>
    <div class="already-min-search">
      <div class="children-1">
        <label for="edit-children">Детей</label>
        <div class="rooms-guests-plus">+</div>
        <input type="text" name="childrenqt" value="0" max="5" min="0" maxlength="5" size="60" maxlength="128" class="form-text">
        <div class="rooms-guests-minus">-</div>
      </div>
    </div>
    <div class="already-min">
      <div class="textfield">
        <label for="edit">Взрослых</label>
        <div class="rooms-guests-plus">+</div>
        <input type="text" name="edit" value="2" max="10" min="0" maxlength="5" size="60" maxlength="128" class="form-text">
        <div class="rooms-guests-minus">-</div>
      </div>
    </div>
  </div>
</div>

大家好,有一个问题,点击加号的时候,添加了已有区块的副本,点击减号的时候,又删除了,最终的结果,应该如图所示,不能添加了超过9个块。图片 1 图片 2

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    NeedHate
    2020-12-28T19:56:12Z2020-12-28T19:56:12Z

    让我们试试这个(我使用了 ES6 语法,但我认为它不会很难弄清楚):

    const room = document.querySelector('.room-guests-count');
    const form = document.querySelector('.form-open');
    const adultsChildren = document.querySelector('.adults-children');
    const adultsChildrenWrapper = document.querySelector('.adults-children-wrapper');
    
    let formIsOpened = false;
    
    room.onclick = () => {
    	if(!formIsOpened) {
    		form.style.display = 'block'
    	} else {
    		form.style.display = 'none'
    	}
    	formIsOpened = !formIsOpened;
    }
    const toggleRoom = () => {
    	const addRoomButton = document.querySelector('#add-room');
    	const removeRoomButton = document.querySelector('#remove-room');
    	const countRoom = document.querySelector('.form-text');
    	
    	const addSection = () => {
    		const wrapper = document.createElement("div");
    		wrapper.className = "adults-children-wrapper";
    		const template = adultsChildrenWrapper.innerHTML;
    		wrapper.innerHTML = adultsChildrenWrapper.innerHTML;                   
    		adultsChildren.appendChild(wrapper);  
    	}
    	const removeSection = () => {
    		adultsChildren.lastChild.remove();
    	}
    	
    	addRoomButton.onclick = () => {
    		if(countRoom.value == 9) return;
    		countRoom.value++;
    		addSection();
    		
    	}
    	removeRoomButton.onclick = () => {
    		if(countRoom.value == 1) return;
    		countRoom.value--;
    		removeSection();
    	}
    	
    }
    toggleRoom();
    body {
      background: lightblue;
    }
    
    /*ROOM GUESTS COUNT*/
    .room-guests-count {
      width: 34%;
      cursor: pointer;
      margin-right: 4%;
      background: #fff;
      height: 66px;
      margin-right: 152px;
    }
    
    .form-open {
      display: none;
      position: absolute;
      z-index: -1;
      top: 83px;
      background-color: #fff;
      height: auto;
      left: 9px;
      width: 348px;
    }
    
    .form-open:after {
      content: '';
      position: absolute;
      width: 0;
      height: 0;
      border-bottom: 10px solid #fff;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
      right: 300px;
      bottom: 93px;
    }
    
    .room-guests-content {
      padding-top: 23px;
      padding-left: 15px;
    }
    
    .click_hidden_text {
      position: absolute;
      z-index: 1;
      left: 163px;
      top: 73px;
      opacity: 0.8;
    }
    
    .form-search {
      padding: 0;
      margin: 10px 10px 5px 10px;
    }
    
    .form-item {
      padding: 10px 5px;
      clear: both;
      height: 14px;
    }
    
    .edit-rooms {
      font-weight: normal;
      font-size: 13px;
      float: left;
      color: #000;
      margin-right: 5px;
    }
    
    .rooms-guests-plus {
      float: right;
      color: #fff;
      background: #ccc;
      width: 24px;
      height: 24px;
      text-align: center;
      line-height: 24px;
      top: -6px;
      position: relative;
      font-size: 18px;
      cursor: pointer;
    }
    
    .form-text {
      text-align: center;
      width: 6%;
      width: 20px !important;
      -webkit-text-decoration: center;
              text-decoration: center;
      float: right;
      border: 0;
      padding: 0;
      height: auto;
      outline: 0;
      vertical-align: middle;
    }
    
    .textfield label {
      margin-right: 2.5px;
      display: inline;
      font-size: 13px;
    }
    
    .children-1 label {
      display: inline;
      font-size: 13px;
    }
    
    .rooms-guests-minus {
      float: right;
      color: #fff;
      background: #e7e7e7;
      width: 24px;
      height: 24px;
      text-align: center;
      line-height: 24px;
      top: -6px;
      position: relative;
      font-size: 18px;
      cursor: pointer;
    }
    
    .adults-children {
      overflow: hidden;
      border: 1px solid red;
    }
    
    .rooms-id {
      line-height: 40px;
      float: left;
      padding: 5px 0 0 15px;
      color: #000;
    }
    
    .already-min {
      float: right;
      margin: -16px 0px 2px 6px;
      padding: 0;
      background: #fff;
    }
    
    .already-min-search {
      float: right;
      margin: 5px 10px 5px 2px;
    }
    
    .adults-children-wrapper {
      overflow: hidden;
    }
    
    .children-1 {
      padding: 10px 5px;
      clear: both;
      height: 14px;
    }
    
    .edit-children {
      font-weight: normal;
      font-size: 13px;
      float: left;
      color: #000;
      margin-right: 5px;
    }
    
    .textfield {
      clear: both;
      height: 14px;
      margin: 0;
      position: relative;
      margin-top: 32px;
    }
    <div class="room-guests-count">
      <div class="room-guests-content">
        Взрослых
        <span class="ga-count">2</span>
        , Детей 
        <span class="children-count">0</span>
      </div>
    </div>
    <div class="form-open">
      <div class="form-search">
        <div class="form-item">
          <label class="edit-rooms">Количество номеров</label>
          <div class="rooms-guests-plus" id="add-room">+</div>
          <input type="text" name="rooms" max="9" min="1" maxlength="128" class="form-text" value="1" size="60" readonly="readonly">
          <div class="rooms-guests-minus" id="remove-room">-</div>
        </div>
      </div>
      <div class="adults-children">
      <div class="adults-children-wrapper">
        <div class="rooms-id">Номер 1:</div>
        <div class="already-min-search">
          <div class="children-1">
            <label for="edit-children">Детей</label>
            <div class="rooms-guests-plus">+</div>
            <input type="text" name="childrenqt" value="0" max="5" min="0" maxlength="5" size="60" maxlength="128" class="form-text">
            <div class="rooms-guests-minus">-</div>
          </div>
        </div>
        <div class="already-min">
          <div class="textfield">
            <label for="edit">Взрослых</label>
            <div class="rooms-guests-plus">+</div>
            <input type="text" name="edit" value="2" max="10" min="0" maxlength="5" size="60" maxlength="128" class="form-text">
            <div class="rooms-guests-minus">-</div>
          </div>
        </div>
      </div>
    </div>

    来自愿望:有必要处理布局(浮动,向活动/非活动按钮添加类)。

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5