【问题标题】:Changing an element type using jquery使用 jquery 更改元素类型
【发布时间】:2013-12-03 14:10:33
【问题描述】:

我有以下代码:

$('#theForm').on('click', 'button', function(e){
var attrs = { };

var str =e.target.id;
var newStr = str.substring(0, str.length-1);

$("#file01b").replaceWith(function () {
attrs.text = $(this).text();
return $("<a />", attrs);
});

var field= document.getElementById(newStr);
field.value= field.defaultValue;
document.getElementById(newStr).style.display = "none";
});

全部代码:http://jsfiddle.net/K9eWL/3/

当我点击第一个“删除文件”按钮时,它工作正常,所以我尝试自动制作它,以便它适用于所有按钮:

$('#theForm').on('click', 'button', function(e){
var attrs = { };

var str =e.target.id;
var newStr = str.substring(0, str.length-1);

$("#str").replaceWith(function () {
attrs.text = $(this).text();
return $("<a />", attrs);
});

var field= document.getElementById(newStr);
field.value= field.defaultValue;
document.getElementById(newStr).style.display = "none";
});

全部代码:http://jsfiddle.net/K9eWL/3/

但它不起作用,错误在哪里?

感谢您的帮助。

【问题讨论】:

  • doesn't work 不能替代您期望代码执行的简洁说明
  • 如果您使用 jQuery,请使用 jQuery。 document.getElementById(newStr).style.display = "none"; 应该是 $('#'+newStr).hide()

标签: javascript jquery types element


【解决方案1】:

怎么样,而不是:

$("#file01b").replaceWith(function () {

你使用:

$("#"+e.target.id).replaceWith(function () {

检查your fiddle, updated

注意:如果上面没问题,那么$(e.target).replaceWith(function () {work as well

【讨论】:

  • 哦哦,真是个错误!谢谢你,先生。 :)
  • 另外,将行更改为return $("&lt;a /&gt;");,以便删除“Return File”字样。
  • 不,我不能这样做,我需要它,因为,现在我正在更改文本以显示:文件已成功删除。
【解决方案2】:

'this.id' 也有效。

$("#"+this.id).replaceWith(function () {

【讨论】:

    猜你喜欢
    • 2012-01-24
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2014-12-29
    相关资源
    最近更新 更多