【问题标题】:Adding an onclick function to go to url in JavaScript?添加一个 onclick 函数以转到 JavaScript 中的 url?
【发布时间】:2012-10-15 20:04:00
【问题描述】:

当用户将鼠标悬停在某个字段上时,我正在使用这个精美的小 JavaScript 来突出显示该字段。您能否告诉我是否有办法添加一个onclick 函数,该函数将充当链接并转到一个 URL?

<script>
         $(function() {
            $('tr').hover(function() {
                $(this).css('background-color', '#eee');
                $(this).contents('td').css({'border': '0px solid red', 'border-left': 'none', 'border-right': 'none'});
                $(this).contents('td:first').css('border-left', '0px solid red');
                $(this).contents('td:last').css('border-right', '0px solid red');
            },
            function() {
                $(this).css('background-color', '#FFFFFF');
                $(this).contents('td').css('border', 'none');
                $('a#read_message.php').click(function(){ URL(); });
            });
        });
        </script>

【问题讨论】:

  • 我如何获取 url 值,它是一个表格单元格中的链接还是它们都指向同一个 url
  • 你有一个相当奇怪的 id '#read_message.php'?

标签: javascript jquery hyperlink


【解决方案1】:

试试

 window.location = url;

也用

 window.open(url);

如果你想在新窗口中打开。

【讨论】:

    【解决方案2】:
    function URL() {
        location.href = 'http://your.url.here';
    }
    

    【讨论】:

      【解决方案3】:

      在 jquery 中将用户发送到不同的 URL,您可以这样做:

      $("a#thing_to_click").on('click', function(){
           window.location = "http://www.google.com/";    
      });
      

      这种方式也可以,但以上是这些天更新更正确的方式

      $("a#thing_to_click").click(function(e){
               e.preventDefault();
               window.location = "http://www.google.com/";    
      });
      

      【讨论】:

        【解决方案4】:

        不完全确定我理解这个问题,但你的意思是这样的吗?

        $('#something').click(function() { 
            document.location = 'http://somewhere.com/';
        } );
        

        【讨论】:

        • 是的,基本上 javascript 设置为在用户滚动/悬停在表格内容/信息上时突出显示表格字段。我想让突出显示的字段看起来是可点击的,因此它们链接到一个页面。如果这更有意义。对不起,如果没有。
        • 解决了。只是稍微调整了你的代码,而不是使用 # i put in tr 来表示表格,它就可以工作了。谢谢
        【解决方案5】:

        只需使用这个

        onclick="location.href='pageurl.html';"
        

        【讨论】:

        • 这会在同一窗口中打开链接,而接受的答案会在新窗口中打开。我更喜欢这个答案的方法
        【解决方案6】:

        HTML

        <input type="button" value="My Button" 
        onclick="location.href = 'https://myurl'" />
        

        MVC

        <input type="button" value="My Button" 
        onclick="location.href='@Url.Action("MyAction", "MyController", new { id = 1 })'" />
        

        【讨论】:

          【解决方案7】:

          如果您想在新标签页中打开链接,您可以:

          $("a#thing_to_click").on('click',function(){
              window.open('https://yoururl.com', '_blank');
          });
          

          【讨论】:

            【解决方案8】:

            试试

            location = url;
            

            function url() {
                location = 'https://example.com';
            }
            <input type="button" value="Inline" 
            onclick="location='https://example.com'" />
            
            <input type="button" value="URL()" 
            onclick="url()" />

            【讨论】:

              【解决方案9】:

              如果您正在处理&lt;a&gt; 标签,并且您想中断转到默认的href,您应该改用this

              转到默认网址(雅虎):

              <a href="https://yahoo.com" onclick="location.href='https://google.com';"> 
              

              转到新网址(谷歌)onclick

              <a href="https://yahoo.com" onclick="this.href='https://google.com';">
              

              通过使用this,您将中断当前浏览器onclick 事件并更改href,然后继续使用&lt;a href='... 的默认行为

              【讨论】:

              • 嗨@Darvydas,上面的代码按预期工作,但是如何等到页面加载才能在新加载的url上执行另一个事件。?
              • @FayazMd 不确定您要做什么?通常,如果它是 DOM 内的 JavaScript(当前在 DOM 的元素 a 内)作为网页,则您无法控制下一页(您正在重定向浏览器页面)上的 JS 行为。当用户离开页面时,它就完成了。除非您也控制第二页,并且可能会在那里为每个用户监听页面加载并检查document.referrer 以了解它是否是正确的人。此外,如果您正在为浏览器使用可以监听页面 URL 更改的扩展程序/插件之类的东西,那可能就是另一回事了。
              • 嗨@Darvydas,谢谢你的回复。我想尝试如下。在浏览器控制台中,使用具有自执行功能的 javascript 可能是 1)我想加载我的一个网站 url 和 2)想等到它完全加载 3)这样,该网页有一个我需要的表格提取所有行。我可以执行第 1 步,如果我在已经加载的网站上,那么在控制台中我可以提取表行(在 javascript 代码中)。但是,当我尝试为上述步骤按顺序编写代码时,在第 2 步失败。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多