Javascript JQuery:在单击功能中切换多个文本
我有一个JQuery函数,可以切换数据库中的一列(根据datatables.net)Javascript JQuery:在单击功能中切换多个文本,javascript,jquery,jquery-datatables,Javascript,Jquery,Jquery Datatables,我有一个JQuery函数,可以切换数据库中的一列(根据datatables.net) 您可以使用jQuerytext方法来显示所需的文本 function fnShowHide( iCol ) { $(this).text(function(i, txt) { return txt === 'Show Name' ? 'Hide Name' : 'Show Name'; }); } 另外,最好删除inline事件,并将其分配到javascript文件中 HTML
您可以使用jQuery
text
方法来显示所需的文本
function fnShowHide( iCol ) {
$(this).text(function(i, txt) {
return txt === 'Show Name' ? 'Hide Name' : 'Show Name';
});
}
另外,最好删除inline
事件,并将其分配到javascript文件中
HTML
<a href="javascript:void(0);" data-id="[1]" id="name">Show Name</a>
您可以使用jQuery
text
方法来显示所需的文本
function fnShowHide( iCol ) {
$(this).text(function(i, txt) {
return txt === 'Show Name' ? 'Hide Name' : 'Show Name';
});
}
另外,最好删除inline
事件,并将其分配到javascript文件中
HTML
<a href="javascript:void(0);" data-id="[1]" id="name">Show Name</a>
尝试以下方法:
加成
或者使用jquery进行事件注册并去掉元素上的内联onclick属性,使用data-*属性指定元素的相对数据,并使用jquery数据api获取该数据:
化妆
更新
基于对多个字段的注释支持
$(function () {
$('.fields').click(fnShowHide);
});
var showregex = /^Show/i; //matches starting Show word
var hideregex = /^Hide/i; //matches starting Hide word
function fnShowHide() {
var iCol = $(this).data('icol'); //will give [1]
console.log(iCol);
//Your code
$(this).text(function (_, text) { //now this refers to the element clicked
return showregex.test(text) ? //test if it Show
text.replace(showregex, 'Hide') : //replace it with hide
text.replace(hideregex, 'Show'); // else replace it with show
});
}
尝试以下方法:
加成
或者使用jquery进行事件注册并去掉元素上的内联onclick属性,使用data-*属性指定元素的相对数据,并使用jquery数据api获取该数据:
化妆
更新
基于对多个字段的注释支持
$(function () {
$('.fields').click(fnShowHide);
});
var showregex = /^Show/i; //matches starting Show word
var hideregex = /^Hide/i; //matches starting Hide word
function fnShowHide() {
var iCol = $(this).data('icol'); //will give [1]
console.log(iCol);
//Your code
$(this).text(function (_, text) { //now this refers to the element clicked
return showregex.test(text) ? //test if it Show
text.replace(showregex, 'Hide') : //replace it with hide
text.replace(hideregex, 'Show'); // else replace it with show
});
}
在您要显示/隐藏的列中是否有带“显示名称”的链接?不,这不仅仅是表格上方切换列的链接,而是在您要显示/隐藏的列中有带“显示名称”的链接?不,这不仅仅是表格上方切换列的链接。在问题中,将有多个链接(显示名称、显示地址)用于相同的功能(fnShowHide)所以第一个答案不会work@Jabda你能把字符串放在数据属性中吗?如果可以的话,我们仍然可以让它工作得很好…或者我有另一个检测显示或隐藏单词的解决方案。哪一个更简单,我正试图避免为每个链接使用showhide函数。那么…给你:正如问题中所说,将有多个链接(显示名称、显示地址)用于相同的函数(fnShowHide),因此第一个答案不会work@Jabda你能把字符串放在数据属性中吗?如果可以,我们仍然可以让它工作得很好…或者我有另一个检测显示或隐藏单词的解决方案。更简单的是,我正在尝试避免为每个linkCool使用showhide函数…给你:
function fnShowHide( iCol ) {
//Your code
$(this).text(function(_, text) { //now this refers to the element clicked
return text === 'Show Name' ? 'Hide Name' : 'Show Name';
});
}
<a href="javascript:void(0);" data-icol="[1]" id="name">Show Name</a>
$(function(){
$('#name').click(fnShowHide);
});
function fnShowHide()
{
var iCol = $(this).data('icol'); //will give [1]
//Your code
$(this).text(function(_, text) { //now this refers to the element clicked
return text === 'Show Name' ? 'Hide Name' : 'Show Name';
});
}
$(function () {
$('.fields').click(fnShowHide);
});
var showregex = /^Show/i; //matches starting Show word
var hideregex = /^Hide/i; //matches starting Hide word
function fnShowHide() {
var iCol = $(this).data('icol'); //will give [1]
console.log(iCol);
//Your code
$(this).text(function (_, text) { //now this refers to the element clicked
return showregex.test(text) ? //test if it Show
text.replace(showregex, 'Hide') : //replace it with hide
text.replace(hideregex, 'Show'); // else replace it with show
});
}