我期待替换DOM中的元素.
例如,有一个<a>我想用替换替换的元素<span>.
我该怎么做呢?
我想知道如何用这样的纯javascript更改标签
<span>some text</span>
Run Code Online (Sandbox Code Playgroud)
我想改变它
<div>some text</div>
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做.
以下代码适用于所有Web浏览器,除了IE:
<input type="text" name="passwordLogin" value="Password" onfocus="if(this.value=='Password'){this.value=''; this.type='password'};" onblur="if(this.value==''){this.value='Password'; this.type='text'};" size="25" />
Run Code Online (Sandbox Code Playgroud)
如何为IE修复它?
我做了一些更改,但仍然有错误.
我希望它像这样工作:
<input type="text" name="usernameLogin" value="Email" onfocus="if(this.value=='Email'){this.value=''};" onblur="if(this.value==''){this.value='Email'};" size="25" />
Run Code Online (Sandbox Code Playgroud)
如果我不输入任何东西,它会把价值放回去.
所以我尝试了这个:
<td colspan="2" id="passwordLoginTd">
<input id="passwordLoginInput1" type="text" name="passwordLogin" value="Password" onfocus="passwordFocus()" size="25" />
<input id="passwordLoginInput2" style="display: none;" type="password" name="passwordLogin" value="" onblur="passwordBlur()" size="25" />
</td>
<script type="text/javascript">
//<![CDATA[
passwordElement1 = document.getElementById('passwordLoginInput1');
passwordElement2 = document.getElementById('passwordLoginInput2');
function passwordFocus() {
passwordElement1.style.display = "none";
passwordElement2.style.display = "inline";
passwordElement2.focus();
}
function passwordBlur() {
if(passwordElement2.value=='') {
passwordElement2.style.display = "none";
passwordElement1.style.display = "inline";
passwordElement1.focus(); …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以使用javascript更改HTML元素数据类型?
例如,将img更改为div?我这样做的原因并没有破坏img和创建div是因为元素包含很多内联样式,所以我需要将所有内联样式从img复制到div.PS:这个网站仅适用于iPad,因此跨浏览器兼容性不是问题.
var div = img.cloneNode(true);
div.type = "div";
Run Code Online (Sandbox Code Playgroud)
因此,使用img.cloneNode(true)克隆节点/元素会更容易,然后必须将类型更改为div.我希望这是可能的吗?
如果没有,也许有一种简单的方法可以将一个元素样式复制到另一个元素?也许...
// img is an img HTML element
var div = document.createElement("div");
div.style = img.style;
document.body.removeChild(img); // does that destroy div.style aswell?
Run Code Online (Sandbox Code Playgroud)