Can';当数据为空时,不能使用javascript创建输入
我有一个示例代码Can';当数据为空时,不能使用javascript创建输入,javascript,Javascript,我有一个示例代码 <script type="text/javascript"> function remove_news(id) { try { $('news_list').removeChild($('news_'+id)); $('news_list').removeChild($('input_news_'+id)); if($('news_list') == '') { var input =
<script type="text/javascript">
function remove_news(id) {
try {
$('news_list').removeChild($('news_'+id));
$('news_list').removeChild($('input_news_'+id));
if($('news_list') == '') {
var input = document.createElement('input');
input.setAttribute("type", "hidden");
input.setAttribute("name", "news_id");
input.setAttribute("value", "0");
document.getElementById("news_list").appendChild(input);
}
} catch(err){}
}
</script>
<div id="news_list">
<div id='news_1'> News no 1 <a href='#' onclick='remove_news(1); return false;'>[Delete]</a></div>
<input type="hidden" id="input_news_1" name="news_id" value="1" />
</div>
函数删除新闻(id){
试一试{
$('news_list').removeChild($('news_'+id));
$('news_list')。删除child($('input_news_'+id));
如果($('news_list')=''){
var input=document.createElement('input');
setAttribute(“类型”、“隐藏”);
setAttribute(“名称”、“新闻id”);
input.setAttribute(“值”、“0”);
document.getElementById(“新闻列表”).appendChild(输入);
}
}捕获(错误){}
}
新闻一
当我运行代码时,选择删除链接是结果不显示
如何修复它?您需要使用
#news\u id
等作为jQuery选择器,使用#
:
但是,您不必混合使用jQuery和常规JavaScript。既然您使用的是jQuery,那么最好使用所有jQuery。我不能保证上述方法会奏效
try {
// Instead of removing a child node, just remove the element directly:
$('#news_'+id).remove()
$('#input_news_'+id).remove();
if($('#news_list').html() == '') {
var input = $.create("<input type='hidden' name='news_id' value='0'>");
$('#news_list').append(input);
}
} catch(err){}
试试看{
//不删除子节点,只需直接删除元素:
$('#news'+id).remove()
$('#input_news'+id).remove();
如果($('#新闻列表').html()=''){
变量输入=$.create(“”);
$(“#新闻列表”)。追加(输入);
}
}捕获(错误){}
如果要检查对象是否存在,请不要将jQuery对象与空字符串进行比较
if($('news_list') == '') { ...}
检查长度是否为非零。正如其他人所提到的,您似乎缺少识别元素的哈希
if($('#news_list').length == 0) { ...}
此外,if语句中的逻辑似乎有缺陷。检查是否存在news\u list
,然后尝试通过最后一行中的相同id获取元素。有点难说,因为正如我提到的,您正在根据字符串查询对象。那没有道理
if($('news_list') == '') {
var input = document.createElement('input');
input.setAttribute("type", "hidden");
input.setAttribute("name", "news_id");
input.setAttribute("value", "0");
document.getElementById("news_list").appendChild(input); //doesn't exist?
}
如果我理解正确,并且看到您使用jQuery:
function remove_news(id) {
var newsList = $('#news_list'); //cache news list
$('#news_'+id,newsList).remove(); //then remove the news
$('#input_news_'+id,newsList).remove(); //as well as the accompanying input
if(newsList.length){ //and if there is no news left
$('<input type="hidden">').attr({ //create a hidden input
'name':'news_id',
'value':'0'
}).appendTo(newsList); //and append to list
}
}
函数删除新闻(id){
var newsList=$('#news_list');//缓存新闻列表
$('#news'+id,newsList).remove();//然后删除新闻
$('#input_news'+id,newsList).remove();//以及附带的输入
if(newsList.length){//如果没有新闻了
$('').attr({//创建一个隐藏的输入
'name':'news_id',
“值”:“0”
}).appendTo(新闻列表);//并追加到列表
}
}
$('')
的解释类似于$('').attr({'type':'hidden'})
一些注释:
> function remove_news(id) {
> try {
绝对没有必要在这里尝试。防御性地编码,而不是在有衬垫的牢房里
> $('news_list').removeChild($('news_'+id));
> $('news_list').removeChild($('input_news_'+id));
您似乎在jQuery和普通javascript之间存在冲突,因此:
var news_list = document.getElementById('news_' + id);
news_list && news_list.parentNode.removeChild(news_list);
var input_news_list = document.getElementById('input_news_' + id);
input_news_list && input_news_list.parentNode.removeChild(input_news_list);
>
> if($('news_list') == '') {
您刚刚从DOM中删除了该元素,因此在那里找不到它。将其与空字符串进行比较也是一个糟糕的选择,我认为这是一个测试,以查看元素是否在文档中。因为它是不必要的,所以可以将其移除
> var input = document.createElement('input');
> input.setAttribute("type", "hidden");
> input.setAttribute("name", "news_id");
> input.setAttribute("value", "0");
如果你想做的只是清除价值,为什么要这么做?无论如何,放弃setAttribute调用并直接设置属性更简单:
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'news_id';
input.value = '0';
> document.getElementById("news_list").appendChild(input);
> }
> } catch(err){} }
Ih HTML(为方便起见,我将其包装):
>第一新闻
当您不需要元素时,为什么要使用元素A?使用跨距并适当设置其样式(或者,但愿不要使用按钮):
。。。
您可以使用div的id使函数更通用,这样就不必在调用中传递它。然后,您可能只需将一个侦听器附加到一个祖先,该祖先可以看到单击从何而来,并从那里进行计算(即委托)
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'news_id';
input.value = '0';
> document.getElementById("news_list").appendChild(input);
> }
> } catch(err){} }
> <div id='news_1'> News no 1 <a href='#' onclick='remove_news(1);
> return false;'>[Delete]</a></div>
<div ...><span onclick="remove_news(1);">...</span></div>