简体   繁体   English

JavaScript,使用函数更改HTML中显示的文本 <p> 标签

[英]JavaScript, using a function to change the text displayed in a HTML <p> tag

I have a HTML5 canvas, which is displaying a number of images and a paragraph of text on the page underneath the canvas. 我有一个HTML5画布,该画布在画布下面的页面上显示许多图像和一段文本。 I want the text in the paragraph to be updated to display a different element from a JS array depending on which image the user clicks on. 我希望将段落中的文本更新为显示与JS数组不同的元素,具体取决于用户单击的图像。

Currently, I have a 'mousedown' function that looks like this: 目前,我有一个'mousedown'函数,看起来像这样:

    _mousedown: function(evt) {
    this._setUserPosition(evt);
    var obj = this.getIntersection(this.getUserPosition());
    if(obj && obj.shape) {
        var shape = obj.shape;
        this.clickStart = true;
        shape._handleEvent('mousedown', evt);
        isClickOnImage(evt);
        var id = shape.id;
        selectTip(id);
    }

    //init stage drag and drop
    if(Kinetic.DD && this.attrs.draggable) {
        this._initDrag();
    }
}

I tried using the line var id = shape.id to update the ID that's being passed to the function, so that it will get the correct element from my 'tips' array, but for some reason, when I view the page in the browser, and click on an image, the text beneath the canvas is not updated. 我尝试使用var id = shape.id来更新传递给函数的ID,以便从“提示”数组中获取正确的元素,但是由于某些原因,当我在浏览器中查看页面时,然后单击图像,则画布下方的文本不会更新。 It seems that this function is not updating the 'id' variable to the ID of whichever image has been clicked. 似乎此函数未将'id'变量更新为已单击的图像的ID。

After looking into this, it seems to me that I will want to use a loop inside the 'mousedown' function, that will take the 'id' of the image on which the click has been detected, and loop through my 'sources' array (which is where all of the images have been loaded from the HTML into the JS), checking at each position whether the image stored at that location has the same ID as that of the image that has been clicked on. 在研究了这一点之后,在我看来,我想在“ mousedown”函数内使用一个循环,该循环将使用检测到点击的图像的“ id”,并循环遍历“ sources”数组(将所有图像从HTML加载到JS中),然后在每个位置检查存储在该位置的图像是否具有与单击的图像相同的ID。 If it does, the loop should set the text to the text stored at that position of the array, and if not, it should continue looking through the array until it find it. 如果是这样,则循环应将文本设置为存储在数组该位置的文本,否则,应继续遍历数组直到找到它。 Would this make sense? 这有意义吗? I tried adding the following code to the 'mousedown' function, but it doesn't change the text as I expected: 我尝试将以下代码添加到“ mousedown”函数中,但不会像我期望的那样更改文本:

var imageCheckArray = 0;
        while(imageCheckArray < sources.length){
            if(shape.id == sources[imageCheckArray]){
                selectTip(imageCheckArray);
            } else {
                imageCheckArray++;
            }
        }

Is there something I'm missing from the loop? 循环中缺少什么吗?

The code for the whole function currently looks like this: 整个函数的代码当前如下所示:

_mousedown: function(evt) {
    this._setUserPosition(evt);
    var obj = this.getIntersection(this.getUserPosition());
    if(obj && obj.shape) {
        var shape = obj.shape;
        this.clickStart = true;
        shape._handleEvent('mousedown', evt);
        isClickOnImage(evt);
        /*This line needs to get the element of the sources array that has been selected, 
            and then select the element at the same position from the tips array.*/
        //var id = null;
        var imageCheckArray = 0;
        while(imageCheckArray < sources.length){
            if(shape.id == sources[imageCheckArray]){
                selectTip(imageCheckArray);
            } else {
                imageCheckArray++;
            }
        }

        //var id = 
        //selectTip(id);
    }

    //init stage drag and drop
    if(Kinetic.DD && this.attrs.draggable) {
        this._initDrag();
    }
}

Edit 11/01/2013 @ 16:10 编辑11/01/2013 @ 16:10

The code for selectTip is: selectTip的代码是:

function selectTip(id){
    $("#tipsParagraph").text(tips[id]);
}

and I've put a jsFiddle up here: http://jsfiddle.net/cd8G7/ although the 'result' panel is not showing what I actually see when I view the page in my browser- I get the canvas with all of the images displayed, and the paragraph underneath the canvas shows the text from the first element of my 'tips' array. 而且我在这里放了一个jsFiddle: http : //jsfiddle.net/cd8G7/,尽管“结果”面板没有显示我在浏览器中查看页面时实际看到的内容-我得到了包含所有显示的图片,以及画布下方的段落显示了来自“ tips”数组第一个元素的文本。

Edit 23/01/2013 @ 13:50 编辑23/01/2013 @ 13:50

Here's my isClickOnImage function: 这是我的isClickOnImage函数:

function isClickOnImage(event){
    var clickX = event.clientX;
    var clickY = event.clientY;

    //var imageCheckIteration = 0;
    while(imageCheckIteration < sources.length){
        if((clickX > sources[imageCheckIteration].x && clickX < sources[imageCheckIteration].x + imageWidth) &&
        (clickY > sources[imageCheckIteration].y && clickY < sources[imageCheckIteration].y + imageHeight)){
            /*This is where I need to print the variable that holds the text I want to display, but I need to display its contents
            outside the canvas, in the <p></p> tags below. */
            console.log("Click on image detected");
            document.getElementById("tipsParagraph").innerHTML = sources[imageCheckIteration].data-tip /*tips[imageCheckIteration]*/;
        } else {
            document.getElementById("tipsParagraph").innerHTML = "";
        }
    }
}

What I intended that this function do is, capture the X & Y coordinates of any click on the canvas, and store them in the variables "clickX" and "clickY". 我打算让此函数执行的操作是捕获画布上任何单击的X和Y坐标,并将它们存储在变量“ clickX”和“ clickY”中。 Then, I have a variable called "imageCheckIteration" that has been initialised to 0, and while this variable is less than the length of my "sources" array (which is the array where all of the images have been stored), the function should check whether the click was on an area of the canvas that is covered by one of the images in the array. 然后,我有一个名为“ imageCheckIteration”的变量,该变量已初始化为0,并且此变量小于我的“ sources”数组(存储所有图像的数组)的长度,该函数应检查单击是否在画布上被数组中图像之一覆盖的区域。

If it was, then a console log should display the message "click on image detected", and the line 如果是这样,则控制台日志应显示消息“单击检测到的图像”,并且该行

document.getElementById("tipsParagraph").innerHTML = sources[imageCheckIteration].data-tip;

should set the value of the "tipsParagraph" to be the value of the 'data-tip' attribute of whichever image is at the 'imageCheckIteration' position of the 'sources' array. 应该将“ tipsParagraph”的值设置为“源”数组的“ imageCheckIteration”位置上的任何图像的“ data-tip”属性的值。 If the click was detected on an area of the canvas that does not have an image displayed, then the value of the "tipsParagraph" should be set to hold nothing. 如果在未显示图像的画布区域上检测到单击,则应将“ tipsParagraph”的值设置为不包含任何内容。

However, for some reason, when I view the page in the browser, the 'tipsParagraph' displays the text "This is where the text will be displayed", which is its default value, so that's fine. 但是,由于某种原因,当我在浏览器中查看页面时,“ tipsParagraph”显示文本“这是显示文本的位置”,这是它的默认值,所以很好。 But, when I click on an image, or click anywhere else on the canvas, the text displayed in the 'tipsParagraph' is not updated. 但是,当我单击图像或单击画布上的其他任何位置时,“ tipsParagraph”中显示的文本不会更新。

I can't figure out why this is- can someone point me in the right direction? 我不知道为什么会这样-有人能指出我正确的方向吗? Does it mean that my isClickOnImage(event) function is never being called? 这是否意味着永远不会调用我的isClickOnImage(event)函数?

I simplified the way you are getting a reference to an image through the canvas. 我简化了通过画布获取图像引用的方式。 The trick here is to swap the z-index of the canvas and the image container and grab the reference to the image on the mouse up event. 这里的技巧是交换画布和图像容器的z索引,并在mouse up事件上获取对图像的引用。 I don't know of a clean way to get elements behind a canvas, hence the workaround. 我不知道一种在画布后面获取元素的干净方法,因此无法解决。

$('canvas').bind('mousedown', function(e) {
  $('section').css('z-index', 4);
});

$('img').bind('mouseup', function(e) {
  $('#tipsParagraph').text($(this).attr('id') + ":" + $(this).attr('alt'));
  $('section').css('z-index', 2);
});

The second portion here is grabbing some attributes from the image itself and updating the text inside your div. 第二部分是从图像本身获取一些属性,并更新div中的文本。

You can see more of the solution here . 您可以在此处查看更多解决方案。

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

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