Find and replace certain part of value (jquery)
This might be a little complicated. See code below.
When image is clicked I want to change "MY-ID-NUMBER" in "value" and "src" of object. But I want the other information to remain.
When link is clicked I want to restore "value" and "src" so I can use the same function for next image that is clicked.
Is it possible? Please help!
My HTML:
<img height="186" width="134" alt="4988" src="i123.jpg">
<img height="186" width="134" alt="4567" src="i124.jpg">
<a class="restore-to-default" href="#">DVD</a>
<div class="tdt">
<object width="960" height="540"><param name="movie" value="http://www.domain.com/v3.4/player.swf?file=http://se.player-feed.domain.com/cinema/MY-ID-NUMBER/123-1/><param name="wmode" value="transparent" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed id="player" name="player" type="application/x-shockwave-flash" src="http://www.player.domain.com/v3.4/player.swf?file=http://se.player-feed.domain.com/cinema/MY-ID-NUMBER/123-1/&display_title=over&menu=true&enable_link=true&default_quality=xxlarge&controlbar=over&autostart=true&backcolor=000000&frontcolor=ffffff&share=0&a开发者_如何学Pythonmp;repeat=always&displayclick=play&volume=80&linktarget=_blank" width="960" height="540"allowFullScreen="true" allowScriptAccess="always"></embed></object>
My jquery:
$('img').click(function(){
var alt = $(this).attr("alt");
$('.tdt object').val("alt", alt); // Some how change "MY-ID-NUMBER" into the "alt-value" of image
});
$('a').click(function(){
//restore to "MY-ID-NUMBER"
});
Thanks!
To replace MY-ID-NUMBER
with something else you can use the replace
method residing in JavaScripts String
object, in this case:
// Inside click event handler
var $movieObj = $(".tdt object"); // Cache jQuery object
var imgID = $(this).attr("alt"),
movieVal = $("param[name=movie]", $movieObj).val(),
embedSrc = $("embed", $movieObj).attr("src");
$movieObj
.find("param[name=movie]").val(movieVal.replace(/MY-ID-NUMBER/, imgID)).end()
.find("embed", $movieObj).attr("src", embedSrc.replace(/MY-ID-NUMBER/, imgID)).end();
As Johan pointed out, you should store a default value somewhere. My example only works the first time (since /MY-ID-NUMBER/
doesn't match a numeric ID), but shows you the way to approach your replacing problem.
Yeah, sure. You can just read the value, replace the part you want to replace and set it back (and store the default value if necessary). But you need some rules on what is the part of the string you want to replace, and make sure your application always confirms to that.
And you should somewhere store the default value, I'd do it in the object-thing. something like: $('.tdt object').data('defaultid', defaultid);
If I understand correctly, you would just need to store your defaults as variables:
var default1 = "";
var default2 = "";
$('restore-to-default').click(function(){
// use your defaults in here
});
精彩评论