简体   繁体   English

单击链接以显示下拉列表

[英]Click on link to display a drop down

I just want to display a link "Free Chat" when I click on the link.当我点击链接时,我只想显示一个链接“免费聊天”。 It must display a dropdown with the options "yes" and "no" .它必须显示一个带有选项"yes""no"的下拉菜单。

The link must not go anywhere yet, only when "yes" is selected it must redirect to a page.该链接必须不得go在任何地方,只有在选择"yes"时,才必须重定向到页面。

I currently have the code:我目前有代码:

<a href="" id="link" name="link" onClick="yesnolist(1)">Free Chat</a>

and my Javascript:和我的 Javascript:

<script type="text/JavaScript">
  function yesnolist()
  {
     var e = document.getElementById("link");
     var strUser1 = e.options[e.selectedIndex].value;
     if (strUser1 == "1")
        window.location.href = "http://.........";

     return strUser1;
  }
 </script>

I'm not sure I understand your question perfectly.我不确定我是否完全理解你的问题。

I assume that you have said link:我假设您已经说过链接:

<a href="" id="link" name="link" onClick="yesnolist(1)">Free Chat</a>

Now, when someone clicks, you want it to create a dropdown?现在,当有人点击时,您希望它创建一个下拉菜单吗? I'm not sure if you mean a dropdown right next to the anchor element or replace the anchor element with the dropdown.我不确定您是指锚元素旁边的下拉菜单还是用下拉列表替换锚元素。

I would recommend, at any moment, using jQuery for javascript, but since it seems you're not using it:我会建议,在任何时候,对 javascript 使用 jQuery,但因为您似乎没有使用它:

<a href="#yesno" id="link" name="link">Free Chat</a>
<select id="yesno_id" name="yesno" style="display:none;">
    <option value="1">Yes</option>
    <option value="2">No</option>
</select>

We won't display the dropdown yet, even when it's already there.我们不会显示下拉菜单,即使它已经存在。 Now we add a listener to #link and to #yesno_id (notice that the following code either goes right before the tag or should be placed inside an body onload listener).现在我们向#link 和#yesno_id 添加一个监听器(注意,下面的代码要么就在标签之前,要么应该放在body onload 监听器中)。

<script type="text/javascript">
document.getElementById('link').addEventListener('click', function() {
    var yesno = document.getElementById('yesno_id');
    yesno.style.display = 'block';
    yesno.addEventListener('change', function() {
        if (this.value === '1') {
            window.location.href = "http://.........";
        }
        else {
            this.style.display = 'none';
        }
    }, true);

}, true);
</script>

I didn't test it at all and will most likely have errors (I didn't even write it on an editor), but it should give you the general idea.我根本没有测试它,很可能会有错误(我什至没有在编辑器上写它),但它应该给你一个大致的想法。

Here we're using unobtrusive javascript: we don't add any reference to JavaScript on the HTML.这里我们使用不显眼的 javascript:我们不会在 HTML 上添加对 JavaScript 的任何引用。 We simply have id's and classes, like any HTML with no JS would do.我们只有 id 和类,就像任何没有 JS 的 HTML 一样。 Then we access the HTML elements using JavaScript apart from all that and we bind the click and change event handlers (correspondingly to each element).然后我们使用 JavaScript 访问 HTML 元素,除此之外,我们绑定点击和更改事件处理程序(对应于每个元素)。

If the person clicks on the link and then selects No, it will hide the dropdown, if it says yes it will redirect to the url you said.如果此人单击链接然后选择否,它将隐藏下拉列表,如果它说是,它将重定向到您说的 url。 It would be nice to actually have the link on the anchor's href and cancel the event propagation.将链接实际放在锚点的 href 上并取消事件传播会很好。 Then grab the href from the yesno_id event handler and redirect to that.然后从 yesno_id 事件处理程序中获取 href 并重定向到该事件处理程序。 That way, if JS is disabled, it would directly take you to the page (although it wouldn't "confirm").这样,如果 JS 被禁用,它会直接将您带到页面(尽管它不会“确认”)。

actually i dont know what you want to do..but try below code for your task.实际上我不知道你想做什么..但是尝试下面的代码来完成你的任务。

<a href="" id="link" name="link" onClick="yesnolist()">Free Chat</a>


<script type="text/JavaScript">
  function yesnolist()
  {
     var e = confirm('are you want to sure...?');

     if (e == ture)
        window.location.href = "http://.........";
      else
        return false;

    }
 </script>

This may helpful for you..这可能对你有帮助..

Thanks.谢谢。

I found this JQuery plugin the other day which I believe will do exactly what you want:前几天我发现了这个 JQuery 插件,我相信它会完全符合您的要求:

http://thrivingkings.com/apprise/ http://thrivingkings.com/apprise/

See the examples on the page for how to do a 'yes/no' box.有关如何执行“是/否”框的信息,请参阅页面上的示例。

Blockquote inintial loading page hide the that div Blockquote初始加载页面隐藏那个div

when click on the div show with delay 1000当单击延迟 1000 的 div 显示时

Blockquote块引用

u ll get like a drop down visiual你会得到像一个下拉视觉

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

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