JQuery更改嵌入Src属性

时间:2013-03-09 12:36:32

标签: javascript jquery embed src

我无法更改<embed> src属性。

(JQuery的)

for (index in Forcast) {
var imageurl = "http://localhost/DesktopVersion/Inc/Images/Weather/";
var imagename = Forcast[0]['icon']+".svg)";
var WeatherIcon = imageurl+imagename;
var parent = $('embed#GetWeatherIcon').parent();
var newImage = "<embed scr=" + WeatherIcon + " />";
var newElement = $(newImage);

$('embed#GetWeatherIcon').remove();
parent.append(newElement);
....

(HTML)

<div>
<embed id="GetWeatherIcon" type="image/svg+xml" />
</div>

(CSS)

#GetWeatherIcon {
height:150px; width:150px; margin:30px 0 0 35px; padding:0; border:0; position:absolute;
}

如果有人可以请指出我需要改变的东西会很棒。非常感谢!

2 个答案:

答案 0 :(得分:2)

那里缺少

newElement变量。

你应该改变

var newImage = "<embed scr="Image" />";

var newImage = "<embed scr=" + Image + " />";
var newElement = $(newImage);

然后应该工作

$('embed#GetWeatherIcon').remove();
parent.append(newElement);

this question

无法更改src属性
$('embed#GetWeatherIcon').attr('src', Image); // NOT WORKING

<强>更新

我尝试了您的最新代码,但我发现了一个问题。您在src属性名称中犯了一个错误,您写了scr,但它无法正常工作。你在var imagename = Forcast[0]['icon']+".svg)";中也有一个错误,可能有错误的结束括号。

这是我的工作代码:

<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF8">
  <meta name="generator" content="HTML hackers, wwww.htmlhackers.com">
  <title>Embed object change</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <style>
    #GetWeatherIcon { 
      height:150px; width:150px; margin:30px 0 0 35px; padding:0; border:0; position:absolute;
    }
  </style>
</head>
<body>
  <script>
    $(document).ready(function() {
      $('#change_btn').click(function() {
        var imageurl = "http://upload.wikimedia.org/wikipedia/commons/e/e8/";
        var imagename = "Svg_example3.svg";
        var WeatherIcon = imageurl+imagename;
        var parent = $('embed#GetWeatherIcon').parent();
        var newImage = "<embed id=\"GetWeatherIcon\" src=\"" + WeatherIcon + "\" type=\"image/svg+xml\"/>";
        var newElement = $(newImage);

        $('embed#GetWeatherIcon').remove();
        parent.append(newElement);
      });
    });
  </script>
  <p id="change_btn">Change</p>
  <div>
    <embed id="GetWeatherIcon" src="http://upload.wikimedia.org/wikipedia/commons/c/c9/Svg_example4.svg" type="image/svg+xml" />
  </div>
</body>
</html>

答案 1 :(得分:0)

我不确定,bt从html中删除src并在源路径中提供其他名称:

var newImage = "<embed scr="new source" />";