我有以下代码
<b class="xyzxterms" style="cursor: default; ">bryant keil bio</b>
Run Code Online (Sandbox Code Playgroud)
如何将b标签替换为标签,h1但保留所有其他属性和信息?
如何从ajax响应中替换html元素?我所知道的是删除元素,如何用ajax响应替换删除的标签?例如:
我有这样的代码:
<ul id="products">
...............
</ul>
Run Code Online (Sandbox Code Playgroud)
当我点击一个按钮时,会对codeginter控制器进行ajax调用,在那里我收到从数据库中提取的新数据并在另一个视图中呈现,该视图从ul开始并在结束ul时结束.
在ajax成功函数中我这样做:
$('#products').remove();
//What to do now here to replace the removed portion with response of ajax?
Run Code Online (Sandbox Code Playgroud) 我有以下html:
<div>
<input type="text" style="xxxx" size="40"/>
<input type="text" style="xxxx" size="100"/>
<input type="text" style="xxxx" size="100"/>
<input type="text" style="xxxx" size="40"/>
</div>
Run Code Online (Sandbox Code Playgroud)
现在我想将大小为"100"的每个输入更改为与输入具有相同样式的textarea.
我试过这个:
$("input[size=100]").each(function(){
//how to replace it?
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在这里的答案中使用了这个解决方案:
$("input[size=100]").each(function() {
var style = $(this).attr('style'), textbox = $(document.createElement('textarea')).attr({
id : $(this).id,
name : $(this).name,
value : $(this).val(),
style : $(this).attr("style"),
"class" : $(this).attr("class"),
rows : 6
}).width($(this).width());
$(this).replaceWith(textbox);
});
Run Code Online (Sandbox Code Playgroud) 我需要更改(只是)h3标签h2,保留所有默认的html元素,这段代码正在删除所有的HTML内容!
<script type="text/javascript">
$(function () {
$('.sideMenu h3').replaceWith(function () {
return "<h2>" + $(this).text() + "</h2>";
});
});
</script>
<div class="sideMenu">
<h3 class="sapanos">
<span></span>
<a href="#" title="Sainos">Sapanos</a>
</h3>
</div>
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
<div class="normal">
<div>
<div>~Content 1~</div>
</div>
</div>
<div class="normal replace">
<a href="#">
<div>~Content 2~</div>
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
使用jQuery,我想用普通标签替换<a>里面的标签,同时保持它的内容.<div class="normal replace"><div>
<div class="normal replace">
<div="result">
<div>~Content 2~</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我有一个html字符串,我想img用figure标签替换我的所有标签.这是我的代码
$('img',$(html)).replaceWith(function(){
var figure = '<figure><img src="'+$(this).src+'"><figcaption>'+$(this).attr('alt')+'</figcaption></figure>';
return $(figure);
});
Run Code Online (Sandbox Code Playgroud)
此代码不起作用.我还希望在执行操作后返回生成的html字符串,但似乎replace只返回已替换的元素.那我该怎么做?