如果window.location.href.indexOf('player=1')添加样式
if window.location.href.indexOf('player=1') add style
如果我的网站url=http://example.com/?player=1然后我找到了那个代码,但我不能使用它如果url=http://example.com/?player=1车身样式将是
body{ background:#000; }
<script type='text/javascript'>
//<![CDATA[
var curl = window.location.href;
if (curl.indexOf('player=1') != -1) {
curl = curl.replace('player=1', 'm=0');
window.location.href = curl;
}
//]]>
</script>
它将player=1替换为m=0,但我需要使用它来添加样式
将其添加到您的脚本中
if(window.location.href.indexOf("example.com/?player=1") > -1){
$('body').css("background-color","#000");
}
操作您自己的代码如下所示。
var curl = window.location.href;
if (curl.indexOf('player=1') != -1) {
$('body').css("background-color","#000");
}
如果要检查的字符串存在于URL中,则需要设置body元素的style
。
if(window.location.href.indexOf("player=1")!=-1)
document.body.style.backgroundColor="#000";
或者,为了避免设置内联样式,如果要检查的字符串存在,则可以创建一个新类并将其应用于正文。
body.player1{
background:#000;
}
if(window.location.href.indexOf("player=1")!=-1)
document.body.classList.add("player1");
开始:
<script type='text/javascript'>
//<![CDATA[
var curl = window.location.href;
if (curl.indexOf('player=1') != -1) {
document.body.style.backgroundColor = "red"
}
//]]>
</script>
您可以利用JS的正则表达式。
实时演示
这是代码(HTML)-
<p>compu lack, Your styling is based on URL string</p>
CSS-
.dynamic-css {
display: inline-block;
background: tomato;
color: #fff;
padding: 5px;
border-radius: 4px;
}
JS-
$(document).ready(function() {
var url = /jsbin/.test(window.location.href);
if (url) {
console.log("Found 'StackOverflow in URL. I will add Style to text now.'")
$('p').addClass('dynamic-css');
}
});
相关文章:
- 如果window.location.href.indexOf('player=1')添加样式
- 如果选中复选框,则添加样式
- 尝试在随机文本和图像脚本中添加样式/更改字体
- 使用javascript为单词添加样式
- IMG悬停添加样式
- 如何在jquery locationpicker插件中为地图添加样式
- Jquery 在 ASP 文本框上添加样式不起作用
- 如何使用JavaScript/jQuery有条件地添加样式表
- 希望在提醒框中添加样式
- struts2 文本区域 仅为一个单词添加样式
- 使用Javascript为样式切换器添加样式表的最佳方法
- 从用户输入向元素添加样式
- Stylus:为在 JavaScript 中生成的 ID 添加样式
- 在 ajax 函数中向 DIV 添加样式更改
- 在更改时使用javascript函数时向html.dropdownlist添加样式
- .html() 在查询中使用 html() 添加样式
- 动态地向 HTML 表添加样式 - 使用 DOM、JS
- 我可以在 innerHTML 中添加样式标签吗?
- angular ng在条件下重复添加样式
- 制作一个Firefox插件来注入一些DIV并添加样式.那'是的