jQuery链接列表
我有一个根据数据库对象动态创建的链接列表,当我按下一个对象时,我需要向下一个html页面传递一个变量 我的代码是:jQuery链接列表,jquery,html,node.js,Jquery,Html,Node.js,我有一个根据数据库对象动态创建的链接列表,当我按下一个对象时,我需要向下一个html页面传递一个变量 我的代码是: sections.forEach(sec => { var li = jQuery(`<a href='/dish.html'><li>${sec.sectionName}</li></a>`) jQuery('#sections').append(li); }); jQuery('#sections')
sections.forEach(sec => {
var li = jQuery(`<a href='/dish.html'><li>${sec.sectionName}</li></a>`)
jQuery('#sections').append(li);
});
jQuery('#sections').click(function() {
//here I need to determine what link was pressed by the user.
});
#sections is an ordered list
谢谢您可以为每个li元素附加动作侦听器,而不是确定按下了哪个链接。 顺便说一句,当您将任何DOM元素分配给变量名时,在变量名的开头预先加上$sign是一种惯例,这就是我这样做的原因
sections.forEach(sec => {
var $li = jQuery(`<a href='/dish.html'><li>${sec.sectionName}</li></a>`);
$li.on('click', () => {
//here you can attach listener for each element. You can reference using:
var $currentElement = $(this);
//now you can operate on current anchor element.
});
jQuery('#sections').append($li);
});
你可以这样做。我不知道你要从哪些数据中提取。我已经提取了其中包含的文本,希望能对您有所帮助
var sections = [{"sectionName":"rahul"},{"sectionName":"rohit"}]; // My dummy Object
sections.forEach(sec => {
var li = jQuery(`<a class="mylinks" href='#'><li>${sec.sectionName}</li></a>`)
jQuery('#sections').append(li);
});
$(document).on("click",".mylinks",function(){
var touched_li = $(this).find("li").text();
alert(touched_li); // Get section name. e.g rahul
});
那么你面临什么问题呢?我更新了我的代码,就像按钮回答一样,现在我不确定如何提取被触摸的特定对象的文本。我不确定如何提取被触摸的特定对象的文本。