简体   繁体   English

Javascript onclick更改变量值

[英]Javascript onclick change variable value

I am new to JavaScript and I need some help. 我是JavaScript新手,需要一些帮助。 I am making a website and I have several images of team members that if clicked (so I'm guessing the onclick event) will change the variable values corresponding to that team member. 我正在制作一个网站,并且有几张团队成员的图像,如果单击这些图像(所以我猜是onclick事件)将更改与该团队成员相对应的变量值。 For instance, if I click on a picture of Bill Gates, in a separate div, I need to have a variable (let's say Name) change value to Bill Gates. 例如,如果我在单独的div中单击Bill Gates的图片,则需要为Bill Gates设置一个变量(例如Name)更改值。 Then, if I click on an image of Steve Jobs, the variables containing Bill Gates' data will get overwritten with the data of Steve Jobs. 然后,如果单击Steve Jobs的图像,则包含Bill Gates数据的变量将被Steve Jobs的数据覆盖。 How do I do this? 我该怎么做呢?

Thank you. 谢谢。

Assuming mark-up like the following: 假设标记如下:

<div id="gallery">
    <img class="people" data-subject="Steve Jobs" src="path/to/imageOfSteve.png" />
    <img class="people" data-subject="Bill Gates" src="path/to/imageOfBill.png" />
</div>
<span id="caption"></span>

Then a relatively simple, and plain JavaScript, approach: 然后是一个相对简单且简单的JavaScript方法:

function identifySubject(image, targetEl) {
    if (!image) {
        return false;
    }
    else {
        var targetNode = document.getElementById(targetEl) || document.getElementById('caption'),
            person = image.getAttribute('data-subject');
            text = document.createTextNode(person);
        if (targetNode.firstChild.nodeType == 3) {
            targetNode.firstChild.nodeValue = person;
        }
        else {
            targetNode.appendChild(text);
        }
    }
}

var images = document.getElementsByClassName('people');
for (var i=0, len=images.length; i<len; i++) {
    images[i].onclick = function(e){
         identifySubject(this, 'caption');
    };
}

JS Fiddle demo . JS小提琴演示

Onclick attribute is right. Onclick属性是正确的。 You need to add the name of a javascript function to the image's onclick attribute (eg <img src="" onclick="changeVariable()"... ). 您需要将javascript函数的名称添加到图像的onclick属性(例如<img src="" onclick="changeVariable()"... )。

If you want text on the page to change depending on who you click on, you'll need to look at selecting divs in Javascript using getElementById() or similar and look at the InnerHTML property. 如果您希望页面上的文本根据您单击的对象而改变,则需要使用getElementById()或类似方法选择Javascript中的div,然后查看InnerHTML属性。

See: http://woork.blogspot.co.uk/2007/10/how-to-change-text-using-javascript.html 请参阅: http : //woork.blogspot.co.uk/2007/10/how-to-change-text-using-javascript.html

Hope this helps 希望这可以帮助

<img src="path/to/image1"  onclick="getValue('bill gates')" />
<img src="path/to/image2"  onclick="getValue('steve jobs')"/>
<div id="show_value"></div>

<script>
function getValue(val){
    document.getElementById('show_value').innerHTML = val
}
</script>

HTML: HTML:

 <div class="member"><img src="billgates.jpg" /><span>Bill Gates bla bla</span></div>
 <div class="member"><img src="stevejobs.jpg" /><span>Steve Jobs bla bla</span></div>
 <div id="variables"></div>

JS: JS:

$(function(){
    $('.member img').click(function(){
        $('#variables').html($(this).closest('span').html());
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM