我正在尝试在特定页面上显示div元素,例如 - example.com/my-account
,现在它显示在我的所有帐户页面上,例如 - example.com/my-account/lost-password
我知道如何使用JavaScript而不是网页,所以有人可以帮助我吗?这就是我用JavaScript做的方法。我只是需要有人帮我在我试图编辑的php页面内工作。
<script>
var cx = window.location;
var curWin = String(cx);
var myAccount = "http://example.com/my-account/";
if (curWin == myAccount){
<div id="banner"><img src="http://img.c5454o.png"></div>
}
</script>
答案 0 :(得分:1)
如果您打开开发人员工具,可以看到为正文分配了类(使用<body <?php body_class(); ?>>
时)。
例如<body class="home page page-id-7 page-template-default">
。
所以从这里开始,你可以告诉css这样做:
#banner {display: none;}
body.page-id-7 #banner {display: block;}
因此,您根本不需要使用Javascript来检测特定页面并显示特定元素。
答案 1 :(得分:0)
如果你想在特定页面上显示div元素,那么在该页面上创建一个唯一的id,如C:\Program Files\nodejs>npm install mysql
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files
\\node_modules\\npm\\bin\\npm-cli.js" "install" "mysql"
npm ERR! node v0.12.4
npm ERR! npm v2.10.1
npm ERR! code ETIMEDOUT
npm ERR! errno ETIMEDOUT
npm ERR! syscall connect
npm ERR! network connect ETIMEDOUT
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad networ
gs.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files
\\node_modules\\npm\\bin\\npm-cli.js" "install" "mysql"
npm ERR! node v0.12.4
npm ERR! npm v2.10.1
npm ERR! Uncaught, unspecified "error" event.
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! C:\Program Files\nodejs\npm-debug.log
C:\Program Files\nodejs>
,然后转到javascript代码并写,
jQuery代码是:
<div id='UniqueId'>
答案 2 :(得分:0)
像这样添加你的条件
var cx = window.location;
if(cx.substr(-11)=="my-account/") {
//then do whatever you want
}
如果你的字符串没有最后一个斜杠,那么..
if(cx.substr(-10)=="my-account") {
//then do whatever you want
}
substr(-11)函数会从最后10个索引中删除你的url字符串 你可以随后申请你的contion。