简体   繁体   English

通过id Javascript获取元素

[英]Get element by id Javascript

I have a list of videos and a video player(div container). 我有一个视频列表和一个视频播放器(div容器)。 Need to show a video in the div container when a link is clicked. 单击链接时需要在div容器中显示视频。 The video codes is given by a third party so I want to created a conditional statement to check what is clicked based on it's id. 视频代码是由第三方提供的,因此我想创建一个条件语句来检查基于其ID单击的内容。 But is not working at all.. I am pretty sure is some syntax missing... Thanks in advance 但是根本无法使用。.我很确定缺少某些语法...在此先感谢

<script type="text/javascript">
function showVideo()
{   

 if(document.getElementById == videoA)
{   
    alert("videoA")
}

 if(document.getElementById == videoB)
{   
    alert("videoB")
}
}
</script>

--------------------
<a href="#" id="vidoeA" onClick="showVideo()">Video A</a>
<a href="#" id="vidoeB" onClick="showVideo()">Video B</a>

You need to pass in the "this" value: 您需要传递“ this”值:

<a href="#" id="vidoeA" onClick="showVideo(this)">Video A</a>

or: 要么:

<a href="#" id="vidoeA" onClick="showVideo('videoA')">Video A</a>

Then in the handler: 然后在处理程序中:

function showVideo(anchor) { // assumes you used "this", like the 1st example
  var videoId = anchor.id;
  //
  // ... show the video or whatever ...
  //
}

You need to call the getElementById and pass in the id of the element as a parameter: 您需要调用getElementById并将元素的id作为参数传递:

if(document.getElementById("videoA"))

The HTML you've included actually have the id misspelled 您包含的HTML实际上将ID拼写错误

<a href="#" id="vidoeA" onClick="showVideo()">Video A</a>
<a href="#" id="vidoeB" onClick="showVideo()">Video B</a>

should be 应该

<a href="#" id="videoA" onClick="showVideo()">Video A</a>
<a href="#" id="videoB" onClick="showVideo()">Video B</a>

The way you've structured your code will show an alert for both elements. 您构建代码的方式将同时显示两个元素的警报。 If you want the id of the element clicked, you will have to get it from the Event object and do a comparison on that basis. 如果要单击元素的ID,则必须从Event对象获取它并在此基础上进行比较。

document.getElementById("videoB")

You want to actually call the getElementById function and pass in the ID as a paramater. 您实际上要调用getElementById函数并将ID作为参数传递。

The current way returns: 当前方式返回:

document.getElementById
>>function getElementById() { [native code] }

So you are actually comparing that function pointer to a variable videoA(which doesn't actually seem to exist in your code). 因此,您实际上是在将该函数指针与变量videoA(在您的代码中实际上似乎并不存在)进行比较。

However judging by your application, I'd actually attach an onclick handler to see which one was clicked. 但是,根据您的应用程序判断,我实际上会附加一个onclick处理程序,以查看单击了哪个处理程序。

<a href="#" id="videoA" onClick="videoEvent(this)">Video A</a>

and in your javascript: 并在您的JavaScript中:

function videoEvent(videoLink){
  // videoLink is now the ahref element
}

If that's really the code you're using, it's not working because you have a typo in it: your HTML declares two IDs, one called vidoeA and the other called vidoeB , whereas your Javascript is testing for the correct spelling. 如果这确实是您正在使用的代码,那么它就不起作用,因为其中包含输入错误:您的HTML声明了两个ID,一个称为vidoeA ,另一个称为vidoeB ,而您的Javascript正在测试正确的拼写。

(You also need to put the ID name in quotes in your JS). (您还需要将ID名称放在JS中的引号中)。

What you actually want is something like this : 你真正需要的是像这样

HTML HTML
Note: Your ID's were spelled incorrectly in your question. 注意:您的ID拼写错误。

<a href="#" id="videoA" onClick="showVideo(this)">Video A</a>
<a href="#" id="videoB" onClick="showVideo(this)">Video B</a>

JavaScript JavaScript的

function showVideo(elem) {
    if(elem.id == "videoA") {
        alert("videoA")
    }
    if(elem.id == "videoB") {
        alert("videoB")
    }
    //or better yet, just:
    //alert(elem.id);
    return false; //Stops you from following the link.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM