Javascript Div标记代码提取器
是否有可能有一个按钮,当点击它时,它将提取特定div标记中的整个代码?在javescript、jquery或php中可能吗 比如说:Javascript Div标记代码提取器,javascript,jquery,Javascript,Jquery,是否有可能有一个按钮,当点击它时,它将提取特定div标记中的整个代码?在javescript、jquery或php中可能吗 比如说: <div class="extactMe"> <a href="http://stackoverflow.com"><img src="icon.png"></a> </div> 例如,如果我单击网页上的“提取”按钮,则从到的整个div标记将被复制到文本区域。请告知,谢谢。您试过了吗 var ext
<div class="extactMe">
<a href="http://stackoverflow.com"><img src="icon.png"></a>
</div>
例如,如果我单击网页上的“提取”按钮,则从
到
的整个div标记将被复制到文本区域。请告知,谢谢。您试过了吗
var extracted = $('.extractMe').html();
您可以通过使用
outerHTML
属性来实现这一点。试试这个:
var divHtml = $('.extractMe').prop('outerHTML');
$('textarea').val(divHtml);
JSFIDLE上的完整示例: 这里不涉及PHP Javascript部分:
$(document).ready(function() {
// when you click the button
$("#button-extract").click( function() {
// get the html of the div
var divContent = $('div.extractMe').html();
// insert the html of the div into the textarea
$("textarea#my-textarea").val(divContent);
});
});
HTML:
<div class="extractMe">
<a href="http://stackoverflow.com"><img src="icon.png">Yo, man. The image is missing.</a><br/>
</div>
<br/>
<button type="button" id="button-extract">Extract</button>
<br/><br/>
<textarea cols="50" rows="10" id="my-textarea">
Its empty at the start. Err, not really.
</textarea>
摘录
一开始是空的。呃,不是真的。
请查看以下jQuery函数以了解它们的功能:
- 。单击()
- .html()
- .val()
- 和jQuery选择器:$(“.class”)和$(“#id”)
div
中获得a
元素。“$('.extractMe').prop('outerHTML');“做到了这一点,但您给出的解决方案是我将来可以使用的,所以也谢谢您:)太棒了,是的,我肯定会关注这些主题,非常感谢你们,伙计们,我真的很感激。感谢您提供完整的代码。现在将其添加为书签:)案例已解决。