[英]get the html of element itself using jquery .html()
How to get the html of element itself using Jquery html. 如何使用Jquery html获取元素本身的html。 In the below code I would like get the input element inside div using JQuery as shwon below 在下面的代码中,我想使用JQuery作为shwon获取div中的输入元素
<div id="content">content div</div>
<input type='text' id="scheduledDate" class="datetime" />
$(function() {
console.log($('#scheduledDate').html('dsadasdasd'));
$('#content').html($('#scheduledDate').html());
});
EDIT: 编辑:
Can I get the $("#scheduledDate") as string which represent the real html code of the input box, because my final requirement is I want to pass it to some other SubView( I am using backboneJS) and eventually use that html code in a dust file. 我可以将$(“#scheduledDate”)作为字符串来表示输入框的真实html代码,因为我的最终要求是我想将它传递给其他一些SubView(我使用的是backboneJS)并最终使用该html代码在尘埃档中。 My original requirement was to get that input field as string so that I can pass it to some other function. 我最初的要求是将输入字段作为字符串,以便我可以将其传递给其他函数。 I know, if I keep it inside a DIV or some other container, I can get the html by using .html method of JQuery. 我知道,如果我将它保存在DIV或其他容器中,我可以使用JQuery的.html方法获取html。 I dont want use some other for that purpose. 我不想为此目的使用其他一些东西。 I am just trying to get html content of the input box itself using it's id. 我只是想用它的id来获取输入框本身的html内容。
If you want to move the input element into div, try this: 如果要将input元素移动到div中,请尝试以下操作:
$('#content').append($('#scheduledDate'));
If you want to copy the input element into div, try this: 如果要将input元素复制到div中,请尝试以下操作:
$('#content').append($('#scheduledDate').clone());
Note: after move or copy element, the event listener may need be registered again. 注意:在move或copy元素之后,可能需要再次注册事件侦听器。
$(function() {
var content = $('#content');
var scheduledDate = $('#scheduledDate');
content.empty();
content.append(scheduledDate.clone());
});
As the original author has stated that they explicitly want the html of the input: 正如原作者所说,他们明确地想要输入的html:
$(function() {
var scheduledDate = $('#scheduledDate').clone();
var temporaryElement = $('<div></div>');
var scheduleDateAsString = temporaryElement.append(scheduledDate).html();
// do what you want with the html such as log it
console.log(scheduleDateAsString);
// or store it back into #content
$('#content').empty().append(scheduleDateAsString);
});
Is how I would implement this. 我是如何实现这一点的。 See below for a working example: https://jsfiddle.net/wzy168xy/2/ 请参阅下面的工作示例: https : //jsfiddle.net/wzy168xy/2/
A plain or pure JavaScript method, can do better... 一个普通的或纯粹的JavaScript方法,可以做得更好......
scheduledDate.outerHTML //HTML5
or calling by 或者打电话
document.getElementById("scheduledDate").outerHTML //HTML4.01 -FF.
should do/return the same, eg: 应该做/返回相同的,例如:
>> '<input id="scheduledDate" type="text" value="" calss="datetime">'
if this, is what you are asking for fiddle 如果是这样,那就是你要求小提琴
ps: what do you mean by "calss" ? ps:“calss”是什么意思? :-) :-)
This can be done the following ways: 这可以通过以下方式完成:
1.Input box moved to the div and the div content remains along with the added input 1.输入框移动到div,div内容与添加的输入一起保留
$(document).ready(function() {
var $inputBox = $("#scheduledDate");
$("#content").append($inputBox);
});
2.The div is replaced with the copy of the input box(as nnn pointed out) 2. div被替换为输入框的副本(如nnn所指出)
$(document).ready(function() {
var $inputBox = $("#scheduledDate");
var $clonedInputBox = $("#scheduledDate").clone();
$("#content").html($clonedInputBox);
});
Div is replaced by the original input box Div由原始输入框替换
$(document).ready(function() { $(document).ready(function(){
var $inputBox = $("#scheduledDate"); $("#content").html($inputBox); });
https://jsfiddle.net/atg5m6ym/4485/ https://jsfiddle.net/atg5m6ym/4485/
EDIT 1: to get the input html as string inside the div itself use this 编辑1:将输入html作为字符串内部的字符串本身使用此
$("#scheduledDate").prop('outerHTML')
This will give the input objects html as string Check this js fiddle and tell if this is what you need 这将输入对象html作为字符串检查这个js小提琴,并告诉你这是否是你需要的
https://jsfiddle.net/atg5m6ym/4496/ https://jsfiddle.net/atg5m6ym/4496/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.