如何使用JavaScript获取while循环(PHP)的值?
我在PHP中有这个while循环如何使用JavaScript获取while循环(PHP)的值?,javascript,php,while-loop,Javascript,Php,While Loop,我在PHP中有这个while循环 方法1-最小变更 onclick='active_del_modal(this)' 你可以用 function active_del_modal(link) { var x = link.getAttribute("rel"); alert(x); } 方法2-更好: window.addEventListener("load",function() { document.querySeletor("table").addEventListe
方法1-最小变更
onclick='active_del_modal(this)'
你可以用
function active_del_modal(link) {
var x = link.getAttribute("rel");
alert(x);
}
方法2-更好:
window.addEventListener("load",function() {
document.querySeletor("table").addEventListener("click",function(e) {
const tgt = e.target, rel = tgt.getAttribute("rel");
if (rel) alert(rel);
})
})
我建议使用数据属性而不是rel:
如果您将id更改为class,则可以执行此操作
window.addEventListener("load",function() {
document.querySeletor("table").addEventListener("click",function(e) {
const tgt = e.target;
if (tgt.classList.contains("get_post_id")) {
const id = tgt.dataset.id;
console.log(id);
}
})
})
使用
使用
echo“删除”;
方法1-最小变更
onclick='active_del_modal(this)'
你可以用
function active_del_modal(link) {
var x = link.getAttribute("rel");
alert(x);
}
方法2-更好:
window.addEventListener("load",function() {
document.querySeletor("table").addEventListener("click",function(e) {
const tgt = e.target, rel = tgt.getAttribute("rel");
if (rel) alert(rel);
})
})
我建议使用数据属性而不是rel:
如果您将id更改为class,则可以执行此操作
window.addEventListener("load",function() {
document.querySeletor("table").addEventListener("click",function(e) {
const tgt = e.target;
if (tgt.classList.contains("get_post_id")) {
const id = tgt.dataset.id;
console.log(id);
}
})
})
使用
使用
echo“删除”;
- 文档中的ID必须是唯一的。因此,停止在循环中使用静态ID
- 链接到某个地方。如果没有导航,请不要使用链接。如果你想点击一些东西来触发JS,请使用按钮。如果您不喜欢按钮的默认外观,请应用CSS
- 帖子ID不是关系的类型。不要滥用
rel
属性。(适当的用法类似于
)如果需要将自定义数据与元素关联,请使用数据-*
属性
- 内在事件属性有一系列与其相关联的GOTCHA。不要使用它们。改用
addEventListener
和friends李>
功能激活模式(事件){
const按钮=event.target;
const id=button.dataset.postId;
console.log({
身份证件
});
}
document.querySelector(“删除按钮”).addEventListener(“单击”,活动的“删除模式”)代码>
按钮{
边界:无;
背景:无;
颜色:蓝色;
光标:指针;
}
按钮:悬停{
文字装饰:下划线;
}
- 删除
- 删除
- 删除
- 文档中的ID必须是唯一的。因此,停止在循环中使用静态ID
- 链接到某个地方。如果没有导航,请不要使用链接。如果你想点击一些东西来触发JS,请使用按钮。如果您不喜欢按钮的默认外观,请应用CSS
- 帖子ID不是关系的类型。不要滥用
rel
属性。(适当的用法类似于
)如果需要将自定义数据与元素关联,请使用数据-*
属性
- 内在事件属性有一系列与其相关联的GOTCHA。不要使用它们。改用
addEventListener
和friends李>
功能激活模式(事件){
const按钮=event.target;
const id=button.dataset.postId;
console.log({
身份证件
});
}
document.querySelector(“删除按钮”).addEventListener(“单击”,活动的“删除模式”)代码>
按钮{
边界:无;
背景:无;
颜色:蓝色;
光标:指针;
}
按钮:悬停{
文字装饰:下划线;
}
- 删除
- 删除
- 删除
@TheCodesee我知道如何在jQuery中实现这一点,但我想学习如何在没有jQuery的情况下实现这一点。@如果您发布您的工作jQuery,PraveenKumar可能会更容易,然后我们可以帮助您将其“切换”到vanillaJS@mplungjan请,你能解释一下吗?也不要在循环中重新使用用户id。ID必须是唯一的。你可以使用一个类instead@PraveenKumar查看我的更新answer@TheCodesee我知道如何在jQuery中实现这一点,但我想学习如何在没有jQuery的情况下实现这一点。@PraveenKumar可能会更容易,如果您发布您的工作jQuery,然后我们可以帮助“切换”到vanillaJS@mplungjan请,你能解释一下吗?也不要在循环中重新使用用户id。ID必须是唯一的。你可以使用一个类instead@PraveenKumar查看我的最新答案