显示从JAVA到HTML href的可点击链接及其URL图片/文字等

时间:2019-05-23 16:34:12

标签: javascript html

有一个简单的计算,即如果所选的2个单选为true,它将显示指向href的正确链接,以供您查找。该链接会在HTML中弹出,但是如果您单击它,它将转到主页

enter code here

some of the HTML 
<div class="alignment">
 <label class="container">Stereo        
  <img class="portimg" src="img/av.svg" alt="astereoout">
   <input type="radio" name="Audio1" value="1" id="astereoout">
  <span class="checkmark"></span>
 </label>
</div>
<div class="alignment">
 <label class="container">Optical
   <img class="portimg" src="img/Optical.svg" alt="aOpticalin">
     <input type="radio" name="Audio2" value="9" id="aOpticalin">
   <span class="checkmark"></span>
  </label>
</div>
<button onclick="Selectport()" id="BtnData" 
class="Selectport">Select</button>

<a class="Results" id="DisplayResults" href=""></a>
</html>

javascript
function Selectport() {

var aOpticalin = document.getElementById("aOpticalin");
var aOpticalout = document.getElementById("aOpticalout");
var astereoout = document.getElementById("astereoout");
var astereoin = document.getElementById("astereoin");
var DisplayResults = document.getElementById("DisplayResults");

var OptoAv = ((astereoin.checked + aOpticalout.checked) && 
"https://en.wikipedia.org/wiki/Digital-to-analog_converter");
DisplayResults.innerHTML = OptoAv;
var AvToOp = ((astereoout.checked + aOpticalin.checked) && 
"https://en.wikipedia.org/wiki/S/PDIF");
DisplayResults.innerHTML += AvToOp;

2 个答案:

答案 0 :(得分:0)

问题是您实际上从未将HREF设置为URL,您仅在innerHTML中显示,并且要追加多个URL。相反,您需要动态创建链接。

首先,我用div替换了您的锚点。然后,我添加了一个可重用的函数来创建链接。

新代码:

        <div id="DisplayResults"></div>

        function create_link(url, target_obj){
            var a = document.createElement('a');
            var linkText = document.createTextNode(url);
            a.appendChild(linkText);
            a.title = url;
            a.href = url;
            target_obj.appendChild(a);
        }

    function Selectport() {

    var aOpticalin = document.getElementById("aOpticalin");
    var aOpticalout = document.getElementById("aOpticalout");
    var astereoout = document.getElementById("astereoout");
    var astereoin = document.getElementById("astereoin");
    var DisplayResults = document.getElementById("DisplayResults");

        if(astereoin.checked && aOpticalout.checked){
           create_link("https://en.wikipedia.org/wiki/Digital-to-analog_converter",DisplayResults);
        }

        if(astereoout.checked && aOpticalin.checked){
            create_link("https://en.wikipedia.org/wiki/S/PDIF",DisplayResults);
        }
    }

答案 1 :(得分:0)

//*This is one banner link from Amazon(no spaces), Is it possible to display it in your 
//*code that you did in above
if(astereoin.checked && aOpticalout.checked){
create_link(<iframe style="width:120px;height:240px;" marginwidth="0" 
marginheight="0" scrolling="no" frameborder="0" src="//ws- 
na.amazon-adsystem.com/widgets/q? 
ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=US&source=ac&ref= 
tf_til& ad_type=product_link&tracking_id=whatsmycable- 
20&marketplace=amazon&region=US&placement=B01HGHNCMW&asins=B01HGHNCMW&linkId= 
f3759832fc138a941ade9bde6128b083&show_border=true&link_opens_in_new_window= 
false&price_color=333333&title_color=000000&bg_color=d1d1d1">
</iframe>,DisplayResults);