两个函数don't不能一起工作,但不能出错
Two functions don't work together, but no errors
我有几个javascript函数:
- 显示/隐藏某些表行
- 添加新行
两者都在同一页上工作,但不是在特定情况下。
这是下面代码的小提琴:
/*************** show/hide sections ***************/
$('.arrow').click(function(event) {
var sec_id = $(this).attr('id').split('.')[0]; // admin.link -> admin
if ($(this).closest('td').attr('class') == 'subtitle')
$('.s_'+sec_id).toggle(); // toggle hide/show for 1 item (section)
else
$(this).closest('tr').nextUntil('tr:not(.s_' + sec_id+')').toggle();
});
/*************** Add rows ***************/
$('.add_row').click(function(event) {
var sid = $(this).attr('sid'); // the id of the <tbody>
var tbody = document.getElementById(sid); // the <tbody> to add rows to
// === GENERATE NEW NAMES for inputs
// get the name of the first input in the last row
var rows = tbody.rows;
var rowinput = rows[rows.length-1].getElementsByTagName('INPUT');
// split name into array
var name_piece = rowinput[0].name.split('][');
// create name for next row; check if last row is a blank row
var reg = new RegExp('^[0-9]+$');
if (reg.test(name_piece[1])) // if integer
var iteration = parseInt(name_piece[1], 10) + 1; // convert to int with base 10
else
iteration = 1;
if (iteration < 10)
iteration = '0'+iteration; // add left padding to ensure sort order
var front = 'items['+sid.substring(2)+']['+iteration+']'; // front of input name (remove the 's_' from name)
// END GENERATE NEW NAMES for inputs
// === CREATE ROW
var row = document.createElement('tr'); // create a row
var td1 = document.createElement('td'); // create first cell
td1.setAttribute('colSpan', 2);
td1.innerHTML = '<input type="text" name="'+front+'[desc]" maxlength="100" />';
var td2 = document.createElement('td'); // create second cell
td2.innerHTML = '<input type="text" name="'+front+'[price]" maxlength="9" onChange="calc_ttl()" class="right small" />';
var td3 = document.createElement('td'); // create third cell
td3.setAttribute('colSpan', 3);
// END CREATE ROW
// output
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
tbody.appendChild(row);
});
在小提琴中,你会看到3个环节,所有这些环节都能正常工作。唯一的例外是,如果在显示隐藏行的同时添加一行;例如:
- 单击"小节"显示行
- 单击"添加行"
- 单击"小节"隐藏行<--此处失败
从那时起,除非我重新加载页面,否则"小节"链接将不再有效。代码检查出来了,Firebug没有报告任何错误,所以我不知所措。非常感谢任何建议。
问题不在您发布的jQuery代码中。它在HTML中。在你的小提琴:
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" class="s_grp_2d_ttl_asset" style="display: none;">
一旦行可见,只要鼠标在其上移动,s_grp_2d_ttl_asset
类就会被highlight
类替换,这将使单击事件停止在第一个元素。如果使用addClass、removeClass或toggleClass函数,则可以在不完全删除原始类的情况下进行更改。
相关文章:
- 转义符不能与innerHTML一起使用
- 三角库可以与firefox一起使用,但不能在Chrome中使用
- "此网站似乎使用滚动链接定位效果.这可能不能很好地与异步平移一起工作;
- 两个独立工作的javascript函数,但不能一起工作
- Jquery animate 不能与 IE 一起使用 - 什么也没发生
- Node http-proxy-middleware 不能与本地服务器一起工作
- 为什么我的css文件不能与MCV项目一起使用
- Rails3-可排序列表不能与wysihtml5一起工作
- 此JavaScript函数可以单独使用,但不能与其他JavaScript函数一起使用
- wrap() 不能很好地与 after() 一起使用
- delay()函数不能与jquery中的addClass()函数一起使用
- jquery draggable和resizable在动态生成的元素中不能一起工作
- Fancybox图像点击可以独立工作,但不能与我的其他代码一起工作
- 为什么JavaScript闭包不能与预定义的函数一起使用
- AngularJS和JS Math.max.apply()不能一起工作
- javascript和php不能一起工作
- CSS 不能与 NodeJS 一起使用
- 不能将 Ruby 数学库与 Opal 一起使用
- 为什么我的JavaScript不能与我的PHP文件一起使用
- jQuery和延迟加载一起不能工作