简体   繁体   English

如何在jQuery中删除DOM元素或从$(this).html()中进行选择?

[英]How to remove a DOM element or select from a $(this).html() in jQuery?

I am trying to get the caret within an html element, either ▲ (▲) and ▼ (▼). 我正在尝试在html元素(▲(▲)和▼(▼))中插入插入符号。

So I have used (it is within a separate function 所以我用过了(它在一个单独的函数中

console.log($(this.html()))

Which returns: 哪个返回:

Phone<span id="sort-caret">▲</span>

Now, if I would like to remove the span entirely from this html element How would i do that? 现在,如果我想从此html元素中完全删除跨度,我该怎么做?

Furthermore if I wanted to select the actual caret for use in comparison how could I do that? 此外,如果我想选择实际的插入符号以进行比较,该怎么做?

Update : 更新

Just to make the answers more relevant. 只是为了使答案更相关。 How can I select the caret from within the html: 如何从html中选择插入符号:

console.log($('#sort-caret', this).html());

Gives: 得到:

undefined

您要删除跨度吗?

$("#sort-caret").remove();

if I would like to remove the span entirely from this html element How would i do that? 如果我想从此html元素中完全删除跨度,我该怎么做?

Using jQuery remove() you can select it and remove it, similar to this: 使用jQuery remove()可以选择它并删除它,类似于:

// you can drop the ",this" if context is not important and no repeated ids are in your HTML.
$('#sort-caret', this).remove();

DEMO - Removing the Span 演示 -移除跨度


Furthermore if I wanted to select the actual caret for use in comparison how could I do that? 此外,如果我想选择实际的插入符号以进行比较,该怎么做?

.html() contains the HTML content of the span. .html()包含跨度的HTML内容。 Place it in a variable for later use, similar to this: 将其放在变量中以供以后使用,类似于:

var caret = $('#sort-caret', this).html();

//or

var caret = $('#sort-caret').html();

DEMO - Get caret from span 演示 -从跨距插入符号


Why your code console.log($('#sort-caret', this).html()); 为什么是您的代码console.log($('#sort-caret', this).html()); is not working and returns undefined is impossible to tell without all of the relevant HTML and script. 如果没有所有相关的HTML 脚本,就无法判断返回的结果是否undefined

The #sort-caret element might not exist at the time you are running your code... #sort-caret元素在您运行代码时可能不存在...

or... 要么...

The #sort-caret element might not exist in what ever context this is. #sort-caret元素可能不是什么都方面存在this的。

Again though, without the relevant code it is hard to tell what this is and without all the relevant HTML it is hard to tell if the element exists at the time of the query or should be in that context in the first place or not. 同样,尽管没有相关代码,也很难说出this是什么,而没有所有相关HTML,就很难说出该元素在查询时是否存在,或者该元素是否应该放在该上下文中。

To remove the dom element and just get text: 要删除dom元素并仅获取文本,请执行以下操作:

 var s = 'Phone<span id="sort-caret">▲</span>';
 var r = /<(\w+)[^>]*>.*<\/\1>/gi;
 console.log(s.replace(r,""));

Demo 演示

And to remove the span from DOM itself: 并从DOM本身删除跨度:

 $('#sort-caret').remove(); 

To get span text in current context,use: 要在当前上下文中获取跨度文本,请使用:

 $(this).find('span').html()

Try this 尝试这个

$(this).find('#sort-caret').remove()

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM