我在更改<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;
}


如果有人可以指出我需要改变的地方那就太好了。谢谢一群!

最佳答案

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>

关于javascript - jQuery更改Embeds Src属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15310734/

10-09 16:39