I am trying t o create img captions based on src value match.
JQUERY : What is the best way to extract "Author-ABC" from an img with src value wwww.abcd.com/images/imagename__Author-ABC__.jpg and replace the alt开发者_如何学Go value with this value.
DRUPAL : Is there a way to preprocess this a drupal template function and save the value in img alt attribute?
Ideas? Basho
Assuming that your image name always starts with imagename_
and ends with _.extension
, you could do something like this:
var src = jQuery("#imgId").attr("src");
var imgName = src.replace(/^.*imagename_/,"").replace(/_\.[a-z]+$/, "");
Or, assuming that the URL itself will not contain any underscores, you can do this:
var src = jQuery("#imgId").attr("src");
var imgName = src.replace(/^[^_]+_/,"").replace(/_\.[a-z]+$/, "");
Instead of doing this with JavaScript you could instead hook into Drupal and do all of this when the image is created. I'm guessing you are using a CCK field on a node. With a hook_form_alter
you can add a submit handler for the form. In it you could do the regex needed to pull out the author name from the img file name and add it as alt attribute.
Doing this you get the markup you want upon creation instead of having to depend on javescript to alter it. This is the flexibility that makes Drupal great and is the Drupal way of doing this.
精彩评论