html - How to pass parameters to a javascript function from the element that calls the function? - Stack Overflow

I have a number of <li> items, which call the same onmouseover javascript function. The function

I have a number of <li> items, which call the same onmouseover javascript function. The function needs to extract some data from the element that calls it, to fill some name and tel variables. This data is typed in capitals in the html code below. Any idea on how to do this is really appreciated.

My HTML:

<li id="item1" onmouseover= "onmouseoveragent(this)" >
<a href="some link">
    <span class="hideme">name</span>
</a>
    <p class="hideme"> NAME TO BE PASSED TO JS
    <strong class="tel">NUMBER TO BE PASSED TO JS</strong>
</p>
</li>

MY javascript:

<script language="javascript" type="text/javascript">
    function onmouseoveragent(e) {
        var name = e.?????;
    var tel = e.?????;
};
</script>

I have a number of <li> items, which call the same onmouseover javascript function. The function needs to extract some data from the element that calls it, to fill some name and tel variables. This data is typed in capitals in the html code below. Any idea on how to do this is really appreciated.

My HTML:

<li id="item1" onmouseover= "onmouseoveragent(this)" >
<a href="some link">
    <span class="hideme">name</span>
</a>
    <p class="hideme"> NAME TO BE PASSED TO JS
    <strong class="tel">NUMBER TO BE PASSED TO JS</strong>
</p>
</li>

MY javascript:

<script language="javascript" type="text/javascript">
    function onmouseoveragent(e) {
        var name = e.?????;
    var tel = e.?????;
};
</script>
Share Improve this question edited Jul 20, 2013 at 8:06 suspectus 17.3k8 gold badges53 silver badges61 bronze badges asked Jul 20, 2013 at 7:48 YaldaYalda 6841 gold badge20 silver badges39 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

yes you do something like this

JAVASCRIPT:

var elements = document.getElementsByClassName('data-item');

var mouseoverHandler = function() {
   var name = this.getElementsByClassName('name')[0].textContent,
       tel = this.getElementsByClassName('tel')[0].textContent;
    alert('Name - ' + name + "\nTel - " + tel);
}

for( var i = 0; i < elements.length; i++ ) {
     var current = elements[i];
     current.addEventListener('mouseover', mouseoverHandler);
}

HTML MARKUP:

<li id="item1" class="data-item"> 
    <a href="some link">
        <span class="hideme">name</span>
    </a>

    <p class="hideme">
        <span class="name">John Smith</span>
        <strong class="tel">555-666-777</strong>
    </p>
</li>

<li id="item1" class="data-item"> 
    <a href="some link">
        <span class="hideme">name</span>
    </a>

    <p class="hideme">
        <span class="name">Caprica Smith</span>
        <strong class="tel">545-334-641</strong>
    </p>
</li>

MDN - document.getElementsByClassName();

MDN - element.textContent

It won't be e.something because e is referring to the event that just happened, that has nothing to do the other elements in the DOM

Demo

Well, there is an easier way to do it, just traverse the childNodes of your current hovered element and parse the results. Here is a working JSFiddle of the snippet below(yes, it works with all the LIs matching that structure):

function onmouseoveragent(e) {
    var children = this.childNodes,
        name = null,
        tel = null;
    for (var i = 0; i < children.length; i++) {
        var child = children[i];
        if (child.tagName === 'P') {
            name = child.firstChild.nodeValue; // the first node is the text node
            tel = child.childNodes[1].firstChild.nodeValue; // the strong's text node
            break; // let's stop the iteration, we've got what we needed and the loop has no reason to go on
        }
    }
    console.log(name, tel); // "NAME TO BE PASSED TO JS " "NUMBER TO BE PASSED TO JS"
}

The only difference in HTML is that you need to pass your handler this way:

<li id="item1" onmouseover="onmouseoveragent.call(this, event)">

So this inside the handler will refer to the element and not to the global object.

I suggest you two thing one change the structure of you li tag i.e; make the tag as shown

<li id="item1" class="someClass" >
<a href="some link">
<span class="hideme">name</span>
</a>
<p class="hideme">NAME TO BE PASSED TO JS </p>
<strong class="tel">NUMBER TO BE PASSED TO JS</strong>
</li> 

remove strong from p because when you try to fetch p(data to be passed the strong tag will e along with it so better change it)

and also try jquery it will give you more flexibility and ease of use(what i feel)

$(".someClass").mouseover(function(e){
    var name = $(e.target).find("p:first").html()
    var tel = $(e.target).find("strong:first").html()

})

try this

function onmouseoveragent(e) {

    var text = e.getElementsByClassName('hideme')[1].textContent;
      var name = text.split("\n")[0];     var num = text.split("\n")[1];  alert(name);    alert(num); }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744364969a4570657.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信