3

我一直在尝试找到一个简短易懂的 javascript,它根据 URL 显示一个 div,但我找到的所有脚本似乎都不起作用。

我的页面上有这个 div,css 文件隐藏了 div:display:none;

<div id="idofthedivtohide"><span>Success!</span></div>

当我转到 url:contact.php?success 我希望 div 更改为 display: block;

有人可以帮我弄这个吗?许多示例使其可以使用 4 行代码。

我已经尝试过这段代码,但它似乎不起作用:

<script type="text/javascript">
if (window.location.search.search(success)) document.getElementById("idofthedivtohide").style.display = "block"
</script>
4

2 回答 2

5

这是另一种解决方案,更“人性化”:

HTML

<div id="idofthedivtohide">
  <span>Success!</span>
</div>

JavaScript

<script type="text/javascript">
  // Get URL
  var url = window.location.href;
  // Get DIV
  var msg = document.getElementById('idofthedivtohide');
  // Check if URL contains the keyword
  if( url.search( 'success' ) > 0 ) {
      // Display the message
      msg.style.display = "block";
  }
</script>
于 2013-11-10T06:51:27.153 回答
1
<div id="success"><span>Success!</span></div>

var locSearch = window.location.search.substring(1).split('&')[0];
if(locSearch){
    document.getElementById( locSearch ).style.display = "block";
}

以上将处理

http://example.exm/page.html?success  // success

如同

http://example.exm/page.html?success&user=2971024&votes=0 // success
于 2013-11-10T06:29:50.680 回答