帮我写一个js代码,用4个相同的p标签,要求点击id为cc_sub减少id为gnum c_pt的value值1,点击cc_add增加id为gnum c_pt的值1
时间: 2024-02-13 12:02:00 浏览: 24
好的,以下是一个简单的实现:
HTML代码:
```html
<p id="gnum" class="c_pt">0</p>
<button id="cc_sub">-</button>
<button id="cc_add">+</button>
```
JavaScript代码:
```javascript
const gnum = document.getElementById("gnum");
const ccSub = document.getElementById("cc_sub");
const ccAdd = document.getElementById("cc_add");
ccSub.addEventListener("click", function() {
gnum.innerText = parseInt(gnum.innerText) - 1;
});
ccAdd.addEventListener("click", function() {
gnum.innerText = parseInt(gnum.innerText) + 1;
});
```
这段代码首先获取了需要操作的元素,即 `id` 为 `gnum` 的 `p` 标签和 `id` 分别为 `cc_sub` 和 `cc_add` 的两个按钮。
然后对两个按钮分别添加了点击事件监听器,点击 `cc_sub` 按钮则将 `gnum` 的值减 1,点击 `cc_add` 则将其加 1。这里使用了 `innerText` 属性来获取和设置 `p` 标签的文本内容,并将其转换为整型进行加减运算。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
-
- 潮影森美 全硅胶性感甜心胶实体娃娃 159cm 169普通款 - <input type="number" id="gnum" data-id="71790817" class="gnum c_pt" min="1" value="7"> + ¥15200.00 删除
帮我做一个js代码,我点击dl标签class为car car_on时候点一次将他class属性设置为car,再次点击时恢复为car car_on,并判断如果class为car car_on时,将data-id和input的value值用post传值到t.php页面并回调显示在一个id为count的em标签里面,请写出完整的js代码和PHP代码
![](https://s.cccgx.com/t/200x200/images/qu/18976/gallery/m20211019094313_99448.jpg)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
-
- 潮影森美 全硅胶性感甜心胶实体娃娃 159cm 169普通款 - <input type="number" id="gnum" data-id="71790817" class="gnum c_pt" min="1" value="7"/> + ¥15200.00 删除
>帮我做一个js代码,我点击dl标签class为car时候点一次将他class属性设置为car car_on,再次点击时恢复为car,并加上判断如果dl的class为car car_on时,将data-id和input的value值用post传值到t.php页面并回调显示在一个id为c_gprice的em标签里面,把t.php的代码也写出了
![](https://s.cccgx.com/t/200x200/images/qu/18976/gallery/m20211019094313_99448.jpg)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)