使用Jquery删除标签

im9ewurl  于 11个月前  发布在  jQuery
关注(0)|答案(1)|浏览(122)

我有两个标签。我只想用Jquery删除第二个标签,不知道该怎么做。

<canvas id="compoundingChart" height="480" width="1204" style="display: block; height: 240px; width: 602px;" class="chartjs-render-monitor"></canvas>

<canvas id="compoundingChart" height="120"></canvas>

字符串
但是我看到了这段代码

$(function() {
    $("#segement1,#segement2,#segement3").hide()
});


但尝试这样做,它没有工作,

$(function() {
    $("<canvas id="compoundingChart" height="120"></canvas>").hide()
});

pgx2nnw8

pgx2nnw81#

您遇到的是语法错误。此错误肯定会在浏览器的开发控制台上报告。请始终先检查那里。
即使在纠正了语法之后.这 * 创建 * 了一个元素,将其设置为隐藏,然后什么也不做(甚至不将其添加到页面):

$('<canvas id="compoundingChart" height="120"></canvas>').hide()

字符串
与您“看到”的代码相比,它通过id * 选择 * 元素并隐藏它们:

$("#segement1,#segement2,#segement3").hide()


如果您打算使用jQuery来选择元素,那么您最好熟悉jQuery selectors(其中大部分基本上与CSS选择器相同)。
您可以通过id选择元素,但首先需要更正HTML以避免重复使用id值。例如:

<canvas id="compoundingChart" height="480" width="1204" style="display: block; height: 240px; width: 602px;" class="chartjs-render-monitor"></canvas>

<canvas id="compoundingChart2" height="120"></canvas>


现在id值是不同的,你可以使用jQuery选择器来定位你想要的元素的id

$("#compoundingChart2").hide()

**编辑:**如果你不能修改HTML,那么你也可以使用这个:

$('canvas[height="120"]').hide();


在文档中选择元素的方法有很多很多,我们鼓励你去研究和学习更多。在这个例子中,你可以通过标签名(canvas)和一个特定的属性值(height="120")来选择。
当然也要注意,如果你不能纠正HTML,那么你仍然有 * 无效的HTML*,所以HTML/CSS/JS的任何行为都可能是未定义的和特定于浏览器的。

相关问题