简体   繁体   English

如何通过使用href更改网址来调用或显示div?

[英]how to call or display a div by changing url using href?

i am trying to show a div which is currently hide and inside body tag.by changing url using anchor tag attribute href.Like below--- 我正在尝试显示当前隐藏在主体标签内的div。通过使用锚标签属性href。

<a id="ai" href="managevendors" class="tablink" onclick="openCity()">Manage Vendors
</a>
<div class="w3-container city" style="display: none;" id="managevendors">
<h1>hi,how are you</h1>
</div>

when i click on this anchor tag my url will definitely changed.and based on url i wants to display a div. 当我单击此锚标记时,我的网址肯定会更改。根据网址,我想显示一个div。 my js code... 我的js代码...

function openCity() {
    if (window.location.hash == "managevendors") {
    $("#managevendors").show();
    }
}

i dont know why this is not working.but i teide with different way like below.. 我不知道为什么这是行不通的。

<a id="ai" href="#managevendors" class="tablink" onclick="openCity('managevendors')">Manage Vendors
</a>

and js code..... 和js代码.....

function openCity()
{
  if (window.location.hash == "#managevendors") {
    $("#managevendors").show();
    }
}

but i dont want the # sing,how can i solve it.help me experience brothers.thanks in advance. 但是我不想唱歌,我该怎么解决。帮助我先体验一下brothers.thanks。

You must use the hash if you expect the navigation to work. 如果希望导航正常运行,则必须使用哈希。 Once you do that, you don't need to check for it, you can just show the section: 完成后,您无需检查它,只需显示以下部分:

 $("#ai").on("click", openCity); function openCity() { // The only reason this code is running is because the link was clicked. // No need to test for it. $("#managevendors").show(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- You have to include the hash in the href for navigation to work --> <a id="ai" href="#managevendors" class="tablink">Manage Vendors </a> <div class="w3-container city" style="display: none;" id="managevendors"> <h1>hi,how are you</h1> </div> 

I think the problem here is that you are using an anchor tag when you should be using a button. 我认为这里的问题是,当您应该使用按钮时,您正在使用锚标记。 The reason the first way is not working is due to the page refreshing when you click the link. 第一种方法不起作用的原因是由于单击链接时页面刷新。

Try changing your <a> to a <button> 尝试将您的<a>更改为<button>

From what I understand, you most likely need the href="#xyz" with a hash. 据我了解,您很可能需要带哈希的href="#xyz" This will keep clientside logic active without trying to solve the url and take a detour to the server for nothing. 这将使客户端逻辑保持活动状态,而无需尝试解析url,而绕开服务器则一无所获。 If you're going to capture that part, keep it local. 如果要捕获该部分,请将其保留在本地。

I suggest to remove the onclick handler from HTML. 我建议从HTML中删除onclick处理程序。 To capture the link you can use jQuery to keep your HTML "clean". 要捕获链接,您可以使用jQuery使HTML保持“干净”。 If you must, for some odd reason, by all means you can reference onclick="openCity(this)" , so the element you click on is passed directly to openCity . 如果出于某种奇怪的原因,您一定可以引用onclick="openCity(this)" ,那么您单击的元素将直接传递给openCity

// vanilla
function openCity(element){
    var href = element.getAttribute('href'), // expecting a hash here
        id = href.substr(1), // remove the hash
        target = document.getElementById(id);

    target.className += " active";
    return false;
}

As Scott suggests with jQuery: 正如Scott建议使用jQuery:

//$('.tablink').on('click', openCity);
$('.tablink[href^="#"]').on('click', openCity);

Then the function can be made dynamic by referencing the href from the clicked element: 然后,可以通过单击所单击元素的href来使函数动态化:

function openCity(ev){
    var el = $(ev.currentTarget),
        id = el.prop('href'), // expecting a hash here
        target = $(id);

    // since you're providing both with and without hash, the default behaviour is to follow the link, unless referenced with a hash
    ev.preventDefault();
    target.addClass('active');
}

If the id was not found on the page, this will void silently. 如果在页面上未找到该ID,则该消息将静默无效。


Now if you want to work with the url as entry point, note that this will only happen on load event => share a link and someone clicks on it (with the #xyz attached) or type directly in the address bar. 现在,如果您要使用url作为入口点,请注意,这只会在加载事件=>共享链接时发生,并且有人单击它(附加#xyz )或直接在地址栏中键入。

// 1. bind the event
$(window).load(function(){
    /*loadCity defined here or outside*/

    loadCity();
});

// 2. define what happens
function loadCity(){
    var id = window.location.hash, // expecting a hash here
        target = $(id);

    target.addClass('active');
}

Since this solution only solves state of an element, the actual show/hide part can be made in CSS. 由于此解决方案仅解决元素的状态,因此可以在CSS中制作实际的显示/隐藏部分。 Very simple as you probably already did or with animations, transitions and so on. 非常简单,就像您可能已经做过的那样,还是使用动画,过渡等。

.city { display: none; }
.city.active { display: block; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM