简体   繁体   English

如何将html属性值添加到javascript变量中

[英]How do I add html attribute value into a javascript variable

Hi I am working on a project that is based on asp and javascript. 嗨,我正在基于ASP和JavaScript的项目上工作。 Also I am new to both programming languages. 我也是这两种编程语言的新手。

I created a popup window that displays a video. 我创建了一个显示视频的弹出窗口。 When you hit its "close" button, the popup would close but the audio kept playing in the background. 当您点击其“关闭”按钮时,弹出窗口将关闭,但音频仍在后台播放。

With this code now my audio stops. 现在有了此代码,我的音频停止了。 But it leads to another issue. 但这导致了另一个问题。

So here is the code. 所以这是代码。

<iframe id="pict" width="560" height="315" src="somelink" frameborder="0"   allowfullscreen></iframe>

I created a javascript variable - to copy the 'iframe src' value to it. 我创建了一个JavaScript变量-将“ iframe src”值复制到该变量。

<script type="javascript"> 
var addurl = document.getElementById('#pict').src;
</script>

Then for 'onclick' in my span, I empty the src (so that the audio stops) and re-add the above variable value back in the src.(so that the user can play the video again in the same session) 然后在我的跨度中单击'onclick',我清空src(以便音频停止),然后将上述变量值重新添加回src中(以便用户可以在同一会话中再次播放视频)

onclick= ""$('#pict').attr('src','');
$('#pict').attr('src','"& addurl & "');"" > 

I dont think the 'addurl' variable is working correctly here. 我不认为'addurl'变量在这里正常工作。 AS I can't play the video twice in the same session. 因为我不能在同一会话中两次播放视频。 Its blank the second time. 第二次空白。

How can I add iframe (src) value inside a variable?? 如何在变量内添加iframe(src)值? I would appreciate if I could get any help to solve this problem. 如果能得到解决该问题的帮助,我将不胜感激。

As mentioned in my comment above, you have an extra > that closes your <iframe> before you set you id, width, height and other attributes. 正如我在上面的评论中所提到的,在设置id,width,height和其他属性之前,有一个额外的>会关闭<iframe> You simply need to remove the extra > and all should be fine. 您只需要删除多余的> ,就可以了。

Simple Demo 简单演示

<iframe> id="pict" width="560" height="315" src="somelink" frameborder="0"   allowfullscreen></iframe>
       ^ - Remove this
<iframe id="pict" width="560" height="315" src="somelink" frameborder="0"   allowfullscreen></iframe>

It looks like you also have some extra quotes "" inside of your javascript and miscellaneous & s. 看来您在javascript和其他& s内也有一些额外的引号""

onclick= ""$('#pict').attr('src','');
         ^^ - Remove the quotes
onclick= $('#pict').attr('src','');

$('#pict').attr('src','"& addurl & "');"" >
                        ^ ------ ^ --- ^^ - Remove the `'`s `&`s and quotes
$('#pict').attr('src',addurl);>

Well, it seems a bit confusing. 好吧,这似乎有点令人困惑。 You have some options here, so I give you a few pointers on how to understand the code better. 您在这里有一些选择,因此,我为您提供了一些如何更好地理解代码的指示。

If you are working inside the page content, you can insert ASP code like this: 如果要处理页面内容,则可以插入如下的ASP代码:

<p>lorem ipsum: "<% =my_value %>" is my value</p>

In your code, it looks, as if you are inside a script block, building a string: 在您的代码中,看起来就像您在脚本块中一样,它构建了一个字符串:

<%
  dim some_asp_variable
  dim my_value

  some_asp_variable = "<p>Lorem ipsum: """ & my_value & """ is my value</p>"

  Response.write some_asp_variable
%>

Note, how you have to write double quotes to put one quote in the string. 注意,如何编写双引号以将一个引号放在字符串中。 and use & to concatenate the strings. 并使用&连接字符串。

The part you have given: 您给出的部分:

 onclick= ""$('#pict').attr('src','');$('#pict').attr('src','"& addurl & "');"" > 

is correct, but only if you are composing a string in ASP (the second option). 是正确的,但前提是您要在ASP中编写字符串(第二个选项)。

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

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