jq删除元素问题

wxdc 2019-10-11 08:26:20
动态删除input

html如下

<input id=1……> <img onclick="aa(this)">
<input id=2……> <img onclick="aa(this)">
.
.
.
<input id=n……> <img onclick="aa(this)">



jq如下:


function aa(x){
$(x).prev("input").remove();
$(x).remove();



这没问题,问题是input和img之间的 ;被遗留了下来。导致页面布局被破坏了。

问:jq如何删除这个 ;谢谢

不要和我说input和img再放入一个标签里面什么的 前提就是2个独立标签的前提下 如何做到删除 ;

...全文
190 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35565360 2019-10-11
  • 打赏
  • 举报
回复
你试一下选定的节点的上一个同级节点删除看看
纨_绔 2019-10-11
  • 打赏
  • 举报
回复
<html lang="zh"> <head> <style> *{ margin: 0; padding: 0; } </style> </head> <body class="light-chip win inited" style="background: rgb(255, 255, 255); --logo-color:rgba(238,238,238,1);"> <input id="1"> <img onclick="aa(this)" src="https://profile.csdnimg.cn/9/8/A/1_u011548042"> <script src="./jQuery.js"></script> <script> function aa(el) { el.previousSibling.remove() $('#1').remove() } </script> </body> </html> 你可以看下大神给出的那个previousSibling方法 这样应该会满足你的需求哈
wxdc 2019-10-11
  • 打赏
  • 举报
回复
引用 5 楼 Logerlink 的回复:
这样

<html lang="zh"><!-- TODO(dbeam): dir="ltr"? --><!-- Copyright 2015 The Chromium Authors. All rights reserved.
     Use of this source code is governed by a BSD-style license that can be
     found in the LICENSE file. --><head></head>
<body class="light-chip win inited" style="background: rgb(255, 255, 255); --logo-color:rgba(238,238,238,1);">
<input id="1……"> <img onclick="aa(this)" src="https://profile.csdnimg.cn/9/8/A/1_u011548042">
<script>
aa = (el)=>{
el.previousSibling.remove()
}
</script>
</body></html>
大神 可以解释一下含义么 我是初学者 看不懂
Logerlink 2019-10-11
  • 打赏
  • 举报
回复

<html lang="zh"><!-- TODO(dbeam): dir="ltr"? --><!-- Copyright 2015 The Chromium Authors. All rights reserved.
     Use of this source code is governed by a BSD-style license that can be
     found in the LICENSE file. --><head></head>
<body class="light-chip win inited" style="background: rgb(255, 255, 255); --logo-color:rgba(238,238,238,1);">
<input id="1……"> <img onclick="aa(this)" src="https://profile.csdnimg.cn/9/8/A/1_u011548042">
<script>
aa = (el)=>{
el.previousSibling.remove()
}
</script>
</body></html>
Logerlink 2019-10-11
  • 打赏
  • 举报
回复
这样

<html lang="zh"><!-- TODO(dbeam): dir="ltr"? --><!-- Copyright 2015 The Chromium Authors. All rights reserved.
     Use of this source code is governed by a BSD-style license that can be
     found in the LICENSE file. --><head></head>
<body class="light-chip win inited" style="background: rgb(255, 255, 255); --logo-color:rgba(238,238,238,1);">
<input id="1……"> <img onclick="aa(this)" src="https://profile.csdnimg.cn/9/8/A/1_u011548042">
<script>
aa = (el)=>{
el.previousSibling.remove()
}
</script>
</body></html>
jio可 2019-10-11
  • 打赏
  • 举报
回复
如果不通过父节点怎么能获取到 ? 没有父级这种情况本来就不存在,不管什么dom都是在html,body里面的,想要删除肯定是可以的。直接用父级包裹更简单,再说 本来都用的很少,随便在dom上加padding,margin都能实现。写代码肯定是简洁好一点,明明一行代码可以谁会去写10行呢?
wxdc 2019-10-11
  • 打赏
  • 举报
回复
引用 2 楼 自渡96 的回复:
[quote=引用 1 楼 wxdc 的回复:] 最后摒弃了 ;在 input中加了margin-right:3px 代替了 ;解决了这个问题 <input id=1…… style='margin-right:3px'><img onclick="aa(this)"> 但是真的需要 jq remove 非元素级别的内容 比如这个 ;的情形下 该如何做呢

<div><input id=1……> <img onclick="aa(this)"></div>

function aa(x){
   $(x).parent().remove();  
}
[/quote] 感谢回复 。但是请看清我的主贴,前提是 input和img 不在一个标签中 是2个独立的标签 如过在一个标签中就不是事了啊
自渡96 2019-10-11
  • 打赏
  • 举报
回复
引用 1 楼 wxdc 的回复:
最后摒弃了 ;在 input中加了margin-right:3px 代替了 ;解决了这个问题 <input id=1…… style='margin-right:3px'><img onclick="aa(this)"> 但是真的需要 jq remove 非元素级别的内容 比如这个 ;的情形下 该如何做呢

<div><input id=1……> <img onclick="aa(this)"></div>

function aa(x){
   $(x).parent().remove();  
}
wxdc 2019-10-11
  • 打赏
  • 举报
回复
最后摒弃了 ;在 input中加了margin-right:3px 代替了 ;解决了这个问题 <input id=1…… style='margin-right:3px'><img onclick="aa(this)"> 但是真的需要 jq remove 非元素级别的内容 比如这个 ;的情形下 该如何做呢
wxdc 2019-10-11
  • 打赏
  • 举报
回复
学习了 previousSibling 文本元素 可以获取兄弟文本元素 previousElementSibling 节点元素 哦 说白了就是标签元素 比较好理解 很多文章翻译都太死板 要自己变通 虽然没有测试成功。但是看了些文章。知道大概了 结贴

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧