Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果id基于某个函数参数,如何访问jquery中的元素?_Javascript_Jquery_Element - Fatal编程技术网

Javascript 如果id基于某个函数参数,如何访问jquery中的元素?

Javascript 如果id基于某个函数参数,如何访问jquery中的元素?,javascript,jquery,element,Javascript,Jquery,Element,我正在尝试使用函数输入访问jquery中的元素。为了澄清这一点,以下是一个我尝试过但不起作用的例子: function openReceivedMessage(messageid) { // ajax post query that is executing fine // set row to not be highlighted var rowid = 'receivedrow' + messageid.toString(); document.getEle

我正在尝试使用函数输入访问jquery中的元素。为了澄清这一点,以下是一个我尝试过但不起作用的例子:

function openReceivedMessage(messageid)
{
    // ajax post query that is executing fine

    // set row to not be highlighted
    var rowid = 'receivedrow' + messageid.toString();
    document.getElementById(rowid).style.background-color = "#ffffff";

    // other code that is executing fine
}
本质上,这是一个邮件收件箱页面。我已经在一个表中显示了消息,随着每个用户的消息数量的变化,我使用了一个循环来填充它。为了打开一条消息,我希望使用一个jquery函数(上面的标题),因此当循环填充表时,我设置它,以便每一个不同的主题行在单击时使用传入的唯一messageid作为参数执行上述函数。打开时,我想更改表中的其他内容(我命名的,类似于message函数,如'receivedrow#',其中#是messageid)


非常感谢这里的任何帮助,我觉得必须有一种简单的方法来创建一个字符串(就像我在上面使用rowid所做的那样)并使用该id访问元素(在表中有一行id=“receivedrow”#“我想调整css)。

我建议使用jQuery来查找元素

var rowid = 'receivedrow' + messageid.toString();
var $el = $("#" + rowid);
然后简单地操作$el

$el.css({'background-color':'#FFFFFF'});

如果您仍然有问题,我建议检查rowid是否正确,jQuery是否返回了正确的元素。

您发布的内容似乎根本不是jQuery:D

获取id为
rowid
的DOM元素是一个简单的JavaScript。它不起作用可能是因为以下两个原因:

  • 没有id为
    rowid
    的元素可以轻松验证
  • 背景色
    不是其
    背景色
    的属性
  • 尝试使用以下方法:

    document.getElementById(rowid).style.backgroundColor = "#ffffff";
    

    如果您真正需要的是一种简单的方法来获得onclick的id,那么只需在行标记中使用this关键字

    onclick="openReceivedMessage(this);"
    
    然后访问您的功能,例如:

    function openReceivedMessage(row)
    {    
        // set row to not be highlighted
        var rowid = 'receivedrow' + row.id;
        $('#' + rowid).css({'background-color':'#ffffff'});
    }
    

    什么不起作用?你试过你的代码了吗?我没有让它起作用,你介意给我看一下上面例子中的片段吗?我想我遗漏了一个撇号或是什么东西,但找不到。明白了,一些语法,我像个白痴一样留在里面,但现在它起作用了。谢谢,很幸运,这里没有运气(我同意没有jQuery在上面的内容,我想知道是否有一种方法可以实现$(“#xxxxx”),其他sme中的哪一个暗示了幸运(我使用了上面的逐字记录,并检查了“receivedrow2”确实是我要寻找的id。还有其他想法吗?我必须想象有一种相对简单的方法可以让这个工作组,你说$(“#receivedrow2”)没有抓住元素?或者$el.css(…)没有做任何事情?
    function openReceivedMessage(row)
    {    
        // set row to not be highlighted
        var rowid = 'receivedrow' + row.id;
        $('#' + rowid).css({'background-color':'#ffffff'});
    }