【问题标题】:Replace Javascript hyperlink with Javascript button用 Javascript 按钮替换 Javascript 超链接
【发布时间】:2015-04-05 02:47:18
【问题描述】:

我是这里的新手。只是用 Javascript 咬牙切齿。我今天花了很长时间浏览每个论坛,我可以尝试找到解决方案。我发现学习这些东西很有趣!

无论如何,我正在使用以下脚本,该脚本与 jw 播放器建立号召性用语链接...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Call to Action</title>
    <script type='text/javascript' src='http://p.jwpcdn.com/6/6/jwplayer.js'>
</script>
    <style type="text/css">
      body { 
        margin: 0; padding: 0 
    }
    </style>
</head>
<body>
<div id="player"></div>
<script type="text/javascript">
jwplayer("player").setup({
  playlist: [{
    image: "http://content.bitsontherun.com/thumbs/w5co0c24-480.jpg",
    sources: [{
      file: "http://content.bitsontherun.com/videos/w5co0c24-hV866gPy.mp4"
    },{
      file: "http://content.bitsontherun.com/videos/w5co0c24-hV866gPy.webm"
    }]
  }],
  width: 600,
  height: 300,
  stretching: "fill"
});

jwplayer().onComplete(function() {
    var el = document.createElement("div");
    var el2 = document.createElement("div");
    var el3 = document.createElement("div");
    var el4 = document.createElement("div");
    var txt = document.createElement('a');
    if (jwplayer().getRenderingMode() == "html5"){
    var theBody = document.getElementById(player.id);
    } else {
    var theBody = document.getElementById(player.id+"_wrapper");
    }
    var playerWidthPX2 = theBody.style.width;
    var playerWidthPX = parseFloat(playerWidthPX2);
    var playerHeightPX2 = theBody.style.height;
    var playerHeightPX = parseFloat(playerHeightPX2);   
    el3.setAttribute('id', 'bg');
    el3.style.height = playerHeightPX + "px";
    el3.style.width = playerWidthPX2;
    el3.style.background = "#333333";
    el3.style.opacity = "0.70";
    el3.style.position = "absolute";
    el3.style.backgroundImage="url('background.png')";
    el.setAttribute('src', 'background.png');
    if (jwplayer().getRenderingMode() == "html5"){
    } else {
    el3.style.top = playerHeightPX-playerHeightPX+"px";
    }
    el3.style.zIndex = "999";
    el3.width = playerWidthPX;
    el3.height = playerHeightPX;
    el2.setAttribute('id', 'bg2');
    el2.style.height = playerHeightPX + "px";
    el2.style.width = playerWidthPX2;
    el2.style.position = "absolute";
    el2.style.zIndex = "999";
    el2.width = playerWidthPX;
    el2.height = playerHeightPX;
    theBody.appendChild(el3); 
    theBody.appendChild(el2);
    el2.style.textAlign = "center";
    el2.style.left = ((playerWidthPX*2)/6) -"5" + "px";
    el2.style.top = ((playerHeightPX*3)/6) -"30" + "px";
    el.setAttribute('id', 'hyperlink');
    el.style.height = "30px";
    el.style.width = "30px";
    el2.width = "30";
    el2.height = "30";
    el.style.position = "relative";
    el.setAttribute('frameBorder', '0');
    el.style.top = "11px";
    el.style.left = "202px";
    el.style.textAlign = "center";
    el.style.marginBottom = "-16px";
    el.style.marginRight = "8px";
    var message = 'Did you like the Trailer?<br />Then <u>see the full movie!    </u>';
    txt.innerHTML = message;
    txt.href = "http://www.youtube.com/watch?v=TEgQM6ZhkWg";
    txt.target = "_blank";
    txt.style.textDecoration = "none";
    txt.style.outline = "0";
    txt.style.MozUserSelect = 'none';
    txt.style.KhtmlUserSelect = 'none';
    txt.style.WebkitUserSelect = 'none';
    txt.style.OUserSelect = 'none';
    txt.style.UserSelect = 'none';
    txt.style.fontSize = "18px";
    txt.style.color = "#ffffff"
    txt.style.position = "absolute";
    txt.style.marginLeft = "6px";
    txt.style.marginTop = "4px";
    txt.style.fontFamily = "arial,_sans";
    txt.setAttribute('id', 'txt');
    el4.setAttribute('id', 'replay');
    el4.style.height = "20px";
    el4.style.width = "20px";
    el4.height = "20";
    el4.width = "20";
    el4.style.position = "absolute";
    el4.style.top = "-" + playerHeightPX/2 + "px";
    el4.style.marginTop = "50px";
    el4.style.left = playerWidthPX/2 + "px";
    el4.style.marginLeft = "50px";
    el4.style.backgroundImage="url('replay.png')";
    el4.setAttribute('src', 'replay.png');
    el2.appendChild(txt);
    el2.appendChild(el);
    el2.appendChild(el4);
    el.style.backgroundImage="url('hyperlink.png')";
    el.setAttribute('src', 'hyperlink.png');
    el.style.cursor = "pointer";
    el.style.display = "table";
    el2.style.display = "table";
    el3.style.display = "table";
    el4.style.display = "table";
    txt.style.display = "table";
    el.onmouseup = function(){
    window.open("http://www.youtube.com/watch?v=TEgQM6ZhkWg");
    }
    el4.style.cursor = "pointer";
    el4.onmouseup = function(){
    el.style.display = "none";
    el2.style.display = "none";
    el3.style.display = "none";
    el4.style.display = "none";
    txt.style.display = "none";
    jwplayer().play();
    }
});
</script>
</body>
</html>

我想用一个按钮替换文本。具体来说,这个按钮...

  <a href="http://www.google.com/"
onMouseOver="return changeImage()"
onMouseOut= "return changeImageBack()"
onMouseDown="return handleMDown()"
onMouseUp="return handleMUp()"
><img
name="jsbutton" src="http://disc.maximumimpact.ca/button/ClickHere-Normal.png"     width="402" height="127" border="0"
alt="javascript button"></a>

<script language="JavaScript">

upImage = new Image();
upImage.src = "http://disc.maximumimpact.ca/button/ClickHere-Rollover.png";
downImage = new Image();
downImage.src = "http://disc.maximumimpact.ca/button/ClickHere-Click.png"
normalImage = new Image();
normalImage.src = "http://disc.maximumimpact.ca/button/ClickHere-Normal.png";

function changeImage()
{
  document.images["jsbutton"].src= upImage.src;
  return true;
}
function changeImageBack()
{
   document.images["jsbutton"].src = normalImage.src;
   return true;
}
function handleMDown()
{
 document.images["jsbutton"].src = downImage.src;
 return true;
}
function handleMUp()
{
 changeImage();
 return true;
}
</script>

有人可以帮忙吗?我希望视频结束,然后按钮出现在视频的中心,背景为灰色。

【问题讨论】:

    标签: javascript jwplayer imagebutton


    【解决方案1】:

    这样做的一个重要方法是删除

    var message = 'Did you like the Trailer?<br />Then <u>see the full movie!    </u>';
    txt.innerHTML = message;
    txt.href = "http://www.youtube.com/watch?v=TEgQM6ZhkWg";
    txt.target = "_blank";
    txt.style.textDecoration = "none";
    txt.style.outline = "0";
    txt.style.MozUserSelect = 'none';
    txt.style.KhtmlUserSelect = 'none';
    txt.style.WebkitUserSelect = 'none';
    txt.style.OUserSelect = 'none';
    txt.style.UserSelect = 'none';
    txt.style.fontSize = "18px";
    txt.style.color = "#ffffff"
    txt.style.position = "absolute";
    txt.style.marginLeft = "6px";
    txt.style.marginTop = "4px";
    txt.style.fontFamily = "arial,_sans";
    txt.setAttribute('id', 'txt');
    

    因为它不再需要。 然后el2.appendChild(txt);之后放

    txt.outerHTML = '<a href="http://www.google.com/" onMouseOver="return changeImage()" onMouseOut= "return changeImageBack()" onMouseDown="return handleMDown()" onMouseUp="return handleMUp()"><img name="jsbutton" src="http://disc.maximumimpact.ca/button/ClickHere-Normal.png"     width="402" height="127" border="0" alt="javascript button"></a>';
    

    它必须在appendChild() 之后发生,因为在将outerHTML 附加到DOM 之前,您无法设置它。您要确保定义了所有事件函数(例如changeImage),因此也将您的脚本放入 HTML。

    执行此操作的更好方法是使用 JavaScript 函数正确地重新创建此按钮。这意味着你会做一些类似的事情

    var button = document.createElement('a');
    button.href = 'http://www.google.com/';
    button.addEventListener('mouseover', changeImage); // Make sure you define changeImage
    ... // Create all the child elements and set their respective properties in JavaScript
    el2.appendChild(button);
    

    【讨论】:

    • 非常感谢您的回复 Wio!非常感谢!
    • 这太棒了!我唯一需要一些指导的是在哪里调整按钮的位置。它在播放器窗口的右侧显得太远了。看这个截图:maximumimpact.ca/images/Screenshot-js.png我会用什么元素来调整它?
    • 你修改它的 top 和 left CSS 属性,或者如果这不起作用修改它的 margin 属性。
    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 2016-08-04
    • 2021-04-02
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    相关资源
    最近更新 更多