简体   繁体   English

无法从具有“ aspNetHidden” CSS类的字符串中删除HTML标签

[英]Unable to remove HTML tags from a string that has a 'aspNetHidden' CSS class

I am compiling a web user control into HTML and it's returning the HTML with view states enclosed in DIVs with class as aspNetHidden . 我正在将一个Web用户控件编译为HTML,它返回的HTML的视图状态包含在类为aspNetHidden DIV中。 But these DIVs are ruining my page's design and I am trying to remove them using following JQuery code: 但是这些DIV破坏了我页面的设计,我试图使用以下JQuery代码将其删除:

var c ='<div class="aspNetHidden"><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="fEButj6b8aiQphz0BrJAJplpNUO1v+7wkyuy4bE4UQ9jy524kYG+mAVtVvxdfpWnKqmCQfVCiHrx21/GiUZe9w==" /></div><div>Code from web user control</div><div class="aspNetHidden"><input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="C21772F6" /></div>';
var $c = $(c);
$c.find('.aspNetHidden').remove();
console.log($c.html());

But this code is returning this: 但是此代码返回此:

<input name="__VIEWSTATE" id="__VIEWSTATE" value="fEButj6b8aiQphz0BrJAJplpNUO1v+7wkyuy4bE4UQ9jy524kYG+mAVtVvxdfpWnKqmCQfVCiHrx21/GiUZe9w==" type="hidden">

Instead of this: 代替这个:

<div>Code from web user control</div>

Please tell me what's going wrong? 请告诉我怎么了? And if possible in C# then please also provide me a server side code too. 如果可能的话在C#中也请提供服务器端代码。

The problem is in line var $c = $(c); 问题在于var $c = $(c); , if you log the initial value of $c ,如果您记录$c的初始值

var $c = $(c);
console.log($c.html())

You would see the problem. 您会看到问题所在。 It just takes the first aspNetHidden as $c and others are ignored. 它只是将第一个aspNetHidden用作$c ,其他则被忽略。

Fiddle 小提琴

The issue is, whenever you wrap a html string with $() , it creates an object with the parent. 问题是,每当您用$()包装html字符串时,它都会与父对象一起创建对象。 And your input c had just 3 <div>s without a parent, so it just takes the first div as input and creates an object. 而且您的输入c只有3个<div>s而没有父级,因此只需将第一个div作为输入并创建一个对象。


Final Code: 最终代码:

Try this, 尝试这个,

var $c = $('<div/>',{html:c});

//or var $c = $('<div'+c+'</div>');

$c.find('.aspNetHidden').remove()

Updated Fiddle 更新小提琴

What exactly do you want to do ? 您到底想做什么?

You are getting just this, because it is first element in an $c array 您正在得到这个,因为它是$c数组中的第一个元素

<input name="__VIEWSTATE" id="__VIEWSTATE" value="fEButj6b8aiQphz0BrJAJplpNUO1v+7wkyuy4bE4UQ9jy524kYG+mAVtVvxdfpWnKqmCQfVCiHrx21/GiUZe9w==" type="hidden">

according to this http://api.jquery.com/html/ $c.html() Get the HTML contents of the first element in the set of matched elements 根据此http://api.jquery.com/html/ $c.html()获取匹配元素集中第一个元素的HTML内容

if you want to get this line 如果你想得到这条线

<div>Code from web user control</div>

try this 尝试这个

var c ='<div class="aspNetHidden"><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="fEButj6b8aiQphz0BrJAJplpNUO1v+7wkyuy4bE4UQ9jy524kYG+mAVtVvxdfpWnKqmCQfVCiHrx21/GiUZe9w==" /></div><div>Code from web user control</div><div class="aspNetHidden"><input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="C21772F6" /></div>';
var $c = $(c);
//$c.find('.aspNetHidden').remove();
console.log($c.get(1));

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

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