jQuery中有什么方法可以获取调用该事件的文档/页面的名称?
以下是on change事件,它是常见的两个页面,两个文档/页面中的元素都具有new_featured_image类
$( document ).on( "change", ".new_featured_image", function() {
// code to get the document/page name
});
无需使用jQuery,您只需阅读
window.location.href
它将为您提供页面地址(应该足以识别该页面)。
无论如何,这并不是实现目标的一种干净方法。最好根据页面(“ pageOne”和“ pageTwo”)向您的“ body”标签添加一个类,然后使用该类来标识页面,例如:
$( document ).on( "change", ".new_featured_image", function() {
if(body.hasClass("pageOne")) {...}
else {...}
});
或添加两个不同的侦听器:
$( document ).on( "change", "body.pageOne .new_featured_image", function() { // code to get the document/page name });
$( document ).on( "change", "body.pageTwo .new_featured_image", function() { // code
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句