首页 文章

将无线电输入和标签转换为可点击的网址字符串

提问于
浏览
-2

请原谅我的新手 . 我只是想了解一些事情 .

<span class="button">
            <input type="radio" id="button-2" name="button-group-1">
            <label onclick="changeButton('2');" for="button-2">Text</label>
        </span>

实际可点击链接与上述内容有什么关系?

例如,以下网址与在Google.com搜索框中输入"stackexchange"相同:https://www.google.com/search?q=stackexchange

如何用第一个例子完成同样的事情?谢谢 .

1 回答

  • 0

    您的代码不会创建可点击的"link",它只是创建一个 label (单击时)激活其相关单选按钮的选择 . <label> 用于辅助功能,因为它可以更轻松地点击小型显示器上的项目,并由屏幕阅读器朗读 .

    onclick 部分用于调用名为 changeButton 的单独JavaScript函数,并将 '2' 字符串传递给它 . 你没有告诉它它做了什么,但它是 labelradio 按钮的单独动作 .

    // Because of the onclick in the HTML of the label, this function will be invoked
    // when you click the label and the data of "2" will be passed to it.
    function changeButton(input){
      console.log("You invoked the changeButton function and passed it the value of: " + input);
    }
    
    <input type="radio" id="button-2" name="button-group-1">
    <label onclick="changeButton('2');" for="button-2">You can click here or on the radio button to activate the radio button, but you can only click on this text to invoke the onclick event associated with the label.</label>
    

    如果要创建实际链接,只需使用HTML <a> 元素,即超链接:

    <a href="https://www.google.com/search?q=stackexchange">Search for Stack Exchange</a>
    

相关问题