![](/img/trans.png)
[英]How to find an element using “substring or contains” of value of HTML Attribute in selector using JQuery?
[英]How to find an element in HTML string and change an attribute using jQuery
这似乎是一个非常简单的问题,但是我已经花了几个小时试图使它起作用。
我有一个像这样的字符串:
var myhtml = '<div id="wrapper"><div id="full" width="800px"></div></div>';
我需要找到我的div #full
,删除它的"*width*"
属性,然后将myhtml与更新的版本一起使用。
我在jQuery中使用它:
var newhtml = $(myhtml).filter("#full").removeAttr("width");
console.log(newhtml );
期望这样: <div id="wrapper"><div id="full"></div></div>
但是它返回"<div id="full"></div>"
而不是整个变量。
使用.find("#full")
选择元素,然后删除属性。 原始对象保留该值。
var myhtml = $('<div id="wrapper"><div id="full" width="800px"></div></div>'); var newhtml = myhtml.find("#full").removeAttr("width"); console.log($(myhtml)[0].outerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.