Jquery 在附加之前更改 val 的 attr
我有一个文本区域字段,用户可以在其中输入 YouTube 嵌入代码,然后在提交时我获取文本区域的 val 并将其附加到正文。我想在将其附加到正文之前将 ?id=123 添加到嵌入代码中 src 的末尾。做到这一点最简单的方法是什么。附加值应该是修改后的 src
嵌入代码看起来像
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/aemXgP-2xyg" frameborder="0" allowfullscreen></iframe>
更新代码的 iframe 代码
var value = $('textarea').val();
var wmode = '?id=123';
var value1 = $(value).attr("src", $(value).attr("src") + wmode);
var code = $('<div class="abs embed" id="embedId">' + value1 + '<span class="move"></span></div>');
$(code).appendTo(...
I have a textarea field where users can enter an youtube embed code, then on submit i grab the val of the textarea and append it to body. I want to add ?id=123 to the end of the src in the embed code before i append it to body. What is the easiest way to do this. The appended value should be the iframe code with the modified src
Embed code looks like
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/aemXgP-2xyg" frameborder="0" allowfullscreen></iframe>
UPDATED CODE
var value = $('textarea').val();
var wmode = '?id=123';
var value1 = $(value).attr("src", $(value).attr("src") + wmode);
var code = $('<div class="abs embed" id="embedId">' + value1 + '<span class="move"></span></div>');
$(code).appendTo(...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(使用 Šime Vidas 的解决方案更新)
编辑:如果您有 firebug,在此页面上,您可以将 iframe 代码复制/粘贴到文本区域中,然后在控制台中运行此代码:
并且 youtube 视频应插入到末尾页面。
编辑2:要在更新中执行您想要的操作,请尝试:
(updated with Šime Vidas' solution)
edit: if you have firebug, on this page, you can copy/paste the iframe code in a textarea, and run this code in the console:
And the youtube video should be inserted at the end of the page.
edit 2: To do what you want in your update, try that:
所有发布的解决方案都很好并且有效,但我更愿意直接删除 src(如果存在),然后查看它是否确实是有效的视频源。如果你让他们输入html,你的风险会很大!
看看Facebook,你可以输入一个链接,他会识别它是什么,如果他可以只传递youtube链接,而不是搜索整个嵌入代码,这对用户来说会好得多!安全第一!
All the posted solutions are good and work but i would prefer to just cut out the src if it exists and then look if it is realy a valid video source. If you let them input html, you are going to risk much!
Look at Facebook, you can enter a link,and he will recognize what it is, and it is much nicer for the user, if he can just pass in the youtube link, not searching for the whole embed code! Safety first!
...其中
s
是要添加到 src 属性的字符串...以及
ts
TEXTAREA 元素... where
s
is the string that you want to add to the src attribute... and
ts
the TEXTAREA element