如何解决如何在 wordpress 上使用 jQuery 更改 <img> attr SRC,因为它具有 <?php bloginfo('template_url') ?>
我想通过单击更改 wordpress 上的图像 attr src。它可以在离线状态下完美运行,但不能在 wordpress 上运行。
点击按钮
<ul>
<li class="bt-color">
<img src="<?php bloginfo( 'template_url' ) ?>/img/blue.png" alt="blue">
</li>
<li class="bt-color">
<img src="<?php bloginfo( 'template_url' ) ?>/img/purple.png" alt="purple">
</li>
<li class="bt-color">
<img src="<?php bloginfo( 'template_url' ) ?>/img/pink.png" alt="pink">
</li>
</ul>
要更改的图像是
//offline
<label for="id1">
<img src="img/main-image.png" />
</label>
//online with wordpress
<label for="id1">
<img src="<?php bloginfo( 'template_url' ) ?>/img/main-image.png" />
</label>
要修改的代码
/* offline code */
$('.bt-color').click(function() {
var alt = ($(this).children().attr('alt'));
if ( alt === 'blue' ){
$("label[for='id1']").children().attr('src','img/main-image-blue.png' );
} else if ( alt === 'purple' ){
$("label[for='id1']").children().attr('src','img/main-image-purple.png' );
}
});
/* online with wordpress,my failed trial */
jQuery('.bt-color').click(function() {
var alt = (jQuery(this).children().attr('alt'));
if ( alt === 'blue' ){
jQuery("label[for='id1']").children().attr('src','<?php bloginfo( 'template_url' ) ?>/img/main-image-blue.png' );
} else if ( alt === 'purple' ){
jQuery("label[for='id1']").children().attr('src','<?php bloginfo( 'template_url' ) ?>/img/main-image-purple.png' );
}
});
离线可以工作,但是,在服务器上在线,它不会替换图像...
我该如何解决?感谢大家阅读并尝试提供帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。