将一个JavaScript字符串转换为一个html对象 [英] converting a javascript string to a html object

查看:105
本文介绍了将一个JavaScript字符串转换为一个html对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将字符串转换为html对象吗?
like:

  string s ='< div id =myDiv>< / div>' ; 
var htmlObject = s.toHtmlObject;

这样我就可以稍后通过id获取它并改变它的风格

  var ho = document.getElementById(myDiv)。style.marginTop = something; 

提前一万美元,
Lina



<$>

p $ p> string s ='< div id =myDiv>< / div>'
var htmlObject = $(s); // jquery call

但仍然不会被 getElementById找到因为要工作,元素必须在DOM中......只是在内存中创建不会插入到DOM中。



您需要在之后使用 append appendTo >等..首先把它放在dom中。

Of'course所有这些都可以通过常规的javascript来完成,但是需要更多的步骤来完成同样的事情。 。这两种情况下的逻辑都是一样的。


can I convert a string to a html object? like:

string s = '<div id="myDiv"></div>';
var htmlObject = s.toHtmlObject;

so that i can later on get it by id and do some changing in its style

var ho = document.getElementById("myDiv").style.marginTop = something;

Thanx a million in advance, Lina

解决方案

You cannot do it with just method, unless you use some javascript framework like jquery which supports it ..

string s = '<div id="myDiv"></div>'
var htmlObject = $(s); // jquery call

but still, it would not be found by the getElementById because for that to work the element must be in the DOM... just creating in the memory does not insert it in the dom.

You would need to use append or appendTo or after etc.. to put it in the dom first..

Of'course all these can be done through regular javascript but it would take more steps to accomplish the same thing... and the logic is the same in both cases..

这篇关于将一个JavaScript字符串转换为一个html对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆