javascript <a> link doesn&#39;t work by clicking

时间:2018-01-22 14:56:17

标签: javascript jquery hyperlink

In order to implement a JavaScript app, we have to implement something like the code below. But once the Google link has been clicked, the user isn't redirected to the www.google.fr.

Do you have the same problem? Any solution?

        $('#myCheckLink').click(function(){
        var x = document.getElementById("myCheckLink").checked;
        if(x === true){
          $('#label').append('<a href="http://www.google.fr" id="link">Google</a>');
        }else{
          $('#link').remove();
        }
            
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="label">
    <input type="checkbox" id="myCheckLink">
    </label>

1 个答案:

答案 0 :(得分:1)

不是附加到您的标签上,也许您应该围绕它添加div并将div附加到target=_blank,样式将保持不变,但现在应该可以正常工作。您也可以尝试将$('#myCheckLink').click(function(){ var x = document.getElementById("myCheckLink").checked; if(x === true){ $('.test').append('<a href="https://www.google.fr" id="link">Google</a>'); }else{ $('#link').remove(); } });与原始代码一起使用。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='test'>
  <label id="label">
    <input type="checkbox" id="myCheckLink">
  </label>  
</div>
<html>
<head><title>asdasd</title>
    <script src="./d3.js"></script>
    <script src="./eventDrops.js"></script>
    <script lang="javascript">
        function test() {
            var hardData = [{
name: 'sensor1',
dates: [
    new Date(1458068401000),
    new Date(1458068401000),
    new Date(1458068403000),
    new Date(1458068404000),
    new Date(1458068405000),
    new Date(1458068406000),
    new Date(1458068411000),
    new Date(1458068413000),
    new Date(1458068415000),
    new Date(1458068416000),
],}]; var eventDropsChart = d3.chart.eventDrops(); d3.select('body')
.datum(hardData)
.call(eventDropsChart);
        }
    </script>
</head>
<body onload="test()"></body></html>

PS:它不适用于stackoverflow。

相关问题